I saw a SO question on here that uses Java Matcher
and Pattern
in an attempt to highlight text, similar to how Regex101 does it highlighting. He's specification was to highlight in a JTextArea for any literal string that is not preceded by a the literal character '#'. I was going to suggest creating your own Matcher
and then the OP deleted his question :(
That was the background, now here is my question. How can I use a regular expression to grab a literal string unless it's after (but not necessary adjacently) a specific string/character in a line?
Example, if I wanted to select the string "tester" from the following
tester, #tester
test tester # test tester
tester
I would hope my regex would select
tester, #tester
test tester # test tester
tester
but not the last "tester".
Using Regex101, the closest I got was /(?=tester)(?<!#)tester/g
but this selects the last "tester" string since I cannot do a "dynamic?" (non-zero) length look back, as far as I can tell.
EDIT:
My question was not Java specific, otherwise I would of placed the Java tag. Unless Regex101 is wrong, I cannot use a Limiting Repetition because "Lookbehinds need to be zero-width, thus quantifiers are not allowed".
I tested WiktorStribiżew regex in Java, and it works fine. Seeing it was a comment and not an answer, All I can do is +1 it, the Java String is (?<!#.{0,1000})\\btester\\b
. I tested it against the following Java String tester, #tester\ntest tester # test testern\tester
Side question, there is no fully defined way to handle regex across all languages? Or is is Regex101 just a poor testing tool (I was using their default, PHP engine)?
I'll consider using RegexStorm or RegexHero in the future.