Kyle Decot
Kyle Decot

Reputation: 20815

Regular expression preceding word match

I trying to make a regular expression that looks for a certain word followed by " not" for example "does not", " are not", etc. There is my regular expression so far: http://rubular.com/r/5bPRtuz9lA

/\b(are|can|could|did|does|do|had||has|should|have|is|were|might|must|will|would)+\snot/i

The problem is that " not" matches this which isn't what I want. I only want it to match if it is preceded by one of the specified words. What am I doing wrong here?

Upvotes: 1

Views: 726

Answers (1)

Paul
Paul

Reputation: 141839

You have an extra pipe here : had||has (It should be had|has).

Which is like saying "had" or "" or "has". So an empty string followed by a space followed by "not" will match your regex.

Btw, you probably don't want the + at the end of your word list. That makes it one or more so your regex will match "canwoulddoes not". Just remove the + to fix that.

Upvotes: 3

Related Questions