【发布时间】:2014-07-05 15:30:18
【问题描述】:
我有一个来自配置文件的 sn-p,我需要能够匹配指定的字符串引用内容,但只有当它们没有被注释掉时,这是我当前的正则表达式:
(?<!=#)test\.this\.regex\s+\"(.*?)\"
我觉得这应该可行吗?我是这样读的:
(?<!=#) 向后查找以确保其前面没有 #
test\.this\.regex\s+\"(.*?)\" 匹配 test.this.regex "sup1"
这里是配置 sn-p
test.this.regex "sup1" hi |sup1| # test.this.regex "sup3" hi |sup3|
# test.this.regex "sup2" do |sup2|
test.this.regex "sup2" do |sup2|
但我的正则表达式匹配所有 4 次:
Match 1
1. sup1
Match 2
1. sup3
Match 3
1. sup2
Match 4
1. sup2
【问题讨论】:
-
您使用哪种语言/工具?
-
#之后似乎有一个空格,您没有在正则表达式中考虑。 -
否定lookbehind的语法是
(?<!...),而不是(?<!=...) -
@anubhava 我正在使用这个rubular.com,因为它支持lookaheads/lookbehinds
-
@bruchowski:用什么语言标记总是好的。标记为ruby、rubular、negative-lookbehind。根据需要重新标记。
标签: ruby regex negative-lookbehind rubular