I have a regex that almost works as it should 🙂
/test[^test<]/
I am using prep_replace() and I want it to find any occurrence of the string ‘test’ and replace it with test except one scenario when the string is ‘test<‘ (means already replaced).
Test text is:
“This is a test. Is this a test? some test and tests but test< testowanie”
It gives me this result:
“This is a test Is this a test some testand tests but test<
testwanie”
So it correctly excludes string ‘test<‘ but with any other matches replaces ‘test’ + one extra character.
Any help would be appreciated. Thanks.
The square brackets represent a character class, and the
^represents negation of that character class. The end result is that you are matching the sequencetestfollowed by any one character that is nott,e,s,tor<Use this instead:
/test(?!<)/, to match all occurrences of the sequencetestnot followed by left angle bracket.