RegEx match part of a string as long as the next character is not _
regex
Solution
You need to use a negative lookahead assertion.
foo(?!_)
The above negative lookahead asserts that the match `foo` won't be followed by an underscore symbol.
DEMO
Problem
I'm sure this is a simple regex, but I couldn't figure out the wording to get what I wanted out of a Google/SO search. In the following string, I want to match foo as long as the next character is not _ (underscore) Lorem ipsum dolfoo_or sit amet, consecteturfoo ut aliquip ex ea commodo consequat. Duis aute irfooure dolor in reprehenderit in volfoo_uptate velit esse cillum dolore eu sunt in culpa qui officfooia deserunt mollit anim id est laborum I should get 3 matches here all foo and ignore the other two foo_ instances. I've got this so far; ``` foo[^_] ``` Which returns this; ``` [0] => Array ( [0] => foo [1] => foou [2] => fooi ) ``` This matches one character after foo, which is not what I want. I don't know how to not match the next character at the same time as saying it must not be and underscore...Please educate me! For reference this is for matching in a text editor, so I'm slightly limited to what I can do, which is why I need to match that exact string and nothing else.