Java regex with positive negative look appearance

I am trying to extract from this type the line ou=persons,ou=(.*),dc=company,dc=org last line that immediately precedes coma , which is not followed by (. *). In the latter case, this should give dc=company,dc=org .

Looking at a regular expression, this seems like a positive look (ahead) of a negative look.

So, I achieved this regular expression: (?<=(,(?!.*\Q(.*)\E))).* , But it returns ,dc=company,dc=org with a coma. I want the same thing without a coma. What am I doing wrong?

+4
source share
2 answers

It seems that I solved the problem on my own by removing the capture group around the negative look ahead. It gives the following regular expression: (?<=,(?!.*\Q(.*)\E)).* .

This is due to the behavior of the captured groups in the environment, as described here: http://www.regular-expressions.info/lookaround.html in the Lookaround Is Atomic part.

+2
source

A comma appears because the capture group contains it.

You can do an external capture group without capture with (?:)

 (?<=(?:,(?!.*\Q(.*)\E))).* 
+2
source

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


All Articles