regex

matching a line that doesn't contain specific text with regular expressions


I want to do the following with regular expressions but not sure how to do it. I want it to match one two when one two is the beginning of the line unless the string contains three anywhere after one two.


Solution

  • You need a negative lookahead assertion - something like this:

    /^one two(?!.*three)/m
    

    Here's a tutorial on lookahead/lookbehind assertions

    Note: I've added the 'm' modifier so that ^ matches the start of a line rather than the start of the whole string.