【发布时间】:2018-02-02 22:40:15
【问题描述】:
我正在尝试解析 XML 文档(特别是 Sublime 颜色主题)并且我正在尝试使用负前瞻来阻止我不想要的匹配,但它似乎无法正常工作.
模式如下:
/
<key>name<\/key>
.*? # find as little as possible including new lines
<string>(.*?)<\/string> # Match the name of this color Rule
.*?
<dict>
((?!<\/dict>).)*? # After the second opening <dict>, do not allow a closing </dict>
<key>foreground<\/key>
.*?
<string>(.*?)<\/string> # Match the hex code for the name found in Match 1.
/mx # Treat a newline as a character matched by .
# Ignore Whitespace, comments.
正在匹配的字符串是:
<dict>
<key>name</key>
<string>**Variable**</string>
<key>scope</key>
<string>variable</string>
<key>settings</key>
<dict>
<key>fontStyle</key>
<string></string>
</dict>
</dict>
<dict>
<key>name</key>
<string>Keyword</string>
<key>scope</key>
<string>keyword - (source.c keyword.operator | source.c++ keyword.operator | source.objc keyword.operator | source.objc++ keyword.operator), keyword.operator.word</string>
<key>settings</key>
<dict>
<key>foreground</key>
<string>**#F92672**</string>
匹配整个字符串,**Variable** 作为第一个捕获的组,**#F92672** 作为第二个。理想情况下,我希望第一个捕获的组是第二部分中的Keyword。我假设负前瞻的存在意味着第一部分不会成为匹配的一部分,因为它会看到 </dict> 并且无法匹配。
有谁知道我是否做错了,我可以做些什么来解决它?谢谢!
【问题讨论】:
标签: ruby regex regex-lookarounds negative-lookahead