Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java regex with a positive look behind of a negative look ahead

I am trying to extract from this kind of string ou=persons,ou=(.*),dc=company,dc=org the last string immediately preceded by a coma not followed by (.*). In the last case, this should give dc=company,dc=org.

Looking on regex, this seems to be a positive look behind (preceded by) of a negative look ahead.

So I have achieve this regex: (?<=(,(?!.*\Q(.*)\E))).*, but it returns ,dc=company,dc=org with the coma. I want the same thing without the coma. What I am doing wrong?

like image 985
Doc Davluz Avatar asked Oct 04 '22 11:10

Doc Davluz


1 Answers

The comma appears because the capturing group contains it.

You can make the outside capture group noncapturing with (?:)

(?<=(?:,(?!.*\Q(.*)\E))).*
like image 184
cyon Avatar answered Oct 10 '22 03:10

cyon