views:

818

answers:

2

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". Note the " marks are just to represent string literals I don't need to match on them.

+2  A: 
^one two(?!.*three)
PEZ
+10  A: 

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.

Paul Dixon