Is it possible to have a regex match that matches the following: I need to know if there is any substring that is inside and outside the corresponding bracket, formatted exactly as (@ ... )
Matching examples:
abc (@ abc )abc (@ (& abc ) )abc (& def (@ abc ) )(& (& abc def ) (@ abc ) )(& def (& abc ) (@ abc ) )
No matching examples:
abc(@ abc )abc (@ def )abc (& abc)
Edit: Here is the rspec specification for validating a regular expression. Only 2 tests are performed:
"(@abc) abc" must match
"(@abc) (@abc)" must not match
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
source share