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
.
The most portable regex would be ^[ \t\n]*$ to match an empty string (note that you would need to replace \t and \n with tab and newline accordingly) and [^ \n\t] to match a non-whitespace string. Save this answer.
How do you ignore something in regex? To match any character except a list of excluded characters, put the excluded charaters between [^ and ] . The caret ^ must immediately follow the [ or else it stands for just itself.
In order to match a line that does not contain something, use negative lookahead (described in Recipe 2.16). Notice that in this regular expression, a negative lookahead and a dot are repeated together using a noncapturing group.
It is used to create a matcher that will match the given input against this pattern. 5. It is used to compile the given regular expression and attempts to match the given input against it.
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.
^one two(?!.*three)
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