Is this possible with regex?

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 
+4
source share
4 answers

Try using a regex pattern

 /(?=.*?(?< !@ )(\b\w+\b)).*\(@.*\1.*\)/ 

See this test code .

+2
source

you can do this quite easily using backlinks:

 /(.*).*\(@ \1 \)/ 

Here, \1 is a reference to what was written in unopened parentheses above.

Your syntax may vary slightly depending on the engine. Perl, sed, vim, etc. All have slight differences in this area. For example, you might need to avoid @ :

 /(.*).*\(\@ \1 \)/ 

Or copy brackets can be escaped:

 /\(.*\).*(@ \1 )/ 

Perl recommends using $1 instead of \1 :

 /(.*).*\(@ $1 \)/ 

Play with him and you are likely to find the right combination.

+2
source

This can do what you want:

 (.+).*\(@.*\1.*\) 

It will only match if any line exists both inside and inside (@ ...)

+1
source

It seems to me that the regular expression is not capable of what you are asking . Your test looks like a test running on a tree, so you need something more powerful like a parser that really turns your string into a tree on which you can run your test.

For this purpose, see parsers such as ANTLR . There are others, but this is one of the most famous that we use.

Edit : examples that may incorrectly match regular expressions: (@ abc) (@ abc) should not match, but I don't see a regular expression that can distinguish it from abc (@ abc) , which matches.

-1
source

Source: https://habr.com/ru/post/1432233/


All Articles