是否有可能有一个匹配以下内容的正则表达式匹配器:我需要知道是否有任何子字符串在匹配括号的内部和外部,完全格式化为(@ ... )
匹配示例:
abc (@ abc )
abc (@ (& abc ) )
abc (& def (@ abc ) )
(& (& abc def ) (@ abc ) )
(& def (& abc ) (@ abc ) )
不匹配的例子:
abc
(@ abc )
abc (@ def )
abc (& abc)
编辑: 这是检查正则表达式的 rspec 规范。只有 2 个测试失败:
"(@abc) abc" 应该匹配
"(@abc) (@abc)" 不应该匹配
describe "regex" do
let(:regex) { /(.+).*\(@.*\1.*\)/ }
matches = ["abc (@ abc )", "(@ abc ) abc", "abc (@ (& abc ) )", "abc (@ (& abc ) )"]
no_matches = ["abc", "(@ abc )", "abc def", "abc abc", "abc (& abc)", "(@ abc ) (@ abc)"]
matches.each do |flow|
it "should match '#{flow}'" do
flow.should match regex
end
end
no_matches.each do |flow|
it "should not match '#{flow}'" do
flow.should_not match regex
end
end
end