I have the following regex to match all words of a text starting with '+'.
Pattern.compile("\\+[\\w-]+");
This works well and matches "+Foo or +Bar" to "+Foo" and "+Bar".
How can I extend the regex to ignore words starting with an escaped '+'-char?
"+Foo or +Bar but no \\+Hello" should match to "+Foo" and "+Bar" but not to "+Hello".
(It should work for JDK1.7.)
Thanks in advance for any help!
You could try a negative lookbehind:
(?<!\\(\\{2})*)\+[\w-]+
In general, (?<!Y)X matches an X that is not preceded by a Y.
Java supports finite-length look-behind, so this should work:
"(?<!\\\\)\\+[\\w-]+"
http://www.regular-expressions.info/lookaround.html
You can use negative look-behind:
Pattern.compile("(?<!\\\\)\\+[\\w-]+");
                        If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With