3.6. Positive and Negative Lookahead
Naturally, a regular expression is matched against a string in a linear fashion (with backtracking as necessary). Therefore there is the concept of the "current location" in the stringrather like a file pointer or a cursor.
The term lookahead refers to a construct that matches a part of the string ahead of the current location. It is a zero-width assertion because even when a match succeeds, no part of the string is consumed (that is, the current location does not change).
In this next example, the string "New World" will be matched if it is followed by "Symphony" or "Dictionary"; however, the third word is not part of the match:
s1 = "New World Dictionary" s2 = "New World Symphony" s3 = "New World Order" reg = /New World(?= Dictionary| Symphony)/ m1 = reg.match(s1) m.to_a # "New World" m2 = reg.match(s2) m.to_a # "New World" m3 = reg.match(s3) # nil
Here is an example of negative lookahead:
reg2 = /New World(?! Symphony)/ m1 = reg.match(s1) m.to_a # "New World" m2 = reg.match(s2) m.to_a # nil m3 = reg.match(s3) # "New World"
In this example, "New World" is matched only if it is not followed by "Symphony."