honeybadger
honeybadger

Reputation: 45

Java regex skip match

I need to capture all # characters in the text except those that are surrounded by #[ ... #]. I wrote the PCRE version (online example) which works great but Java doesn't support (*SKIP)(*FAIL).

 #\[.*#\](*SKIP)(*FAIL)|#

Is there an Java equivalent of this regex? Thanks.

Upvotes: 3

Views: 1630

Answers (1)

lintmouse
lintmouse

Reputation: 5119

This uses a little trick to match the #s you don't want first and then match the rest in a capture group:

#\[.*?#\]|(#+)

https://regex101.com/r/sU1kR2/1

You will need to extract the first capture group to get the desired #s.

If you want to capture each individual # not part of or in the custom brackets, you can drop the + from the capture group as follows:

#\[.*?#\]|(#)

Also, if you can have text like ##[text]#, then you might need a lookaround as follows:

#\[.*?#\]|(#(?!\[))

If you can use \K (but I don't think you can in Java), it is even simpler with the following because then you don't have to worry about capture groups:

#\[.*?#\]\K|#

Upvotes: 2

Related Questions