Section 3.7. Accessing Backreferences


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[0]              # "New World" m2 = reg.match(s2) m.to_a[0]              # "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[0]              # "New World" m2 = reg.match(s2) m.to_a[0]              # nil m3 = reg.match(s3)     # "New World"


In this example, "New World" is matched only if it is not followed by "Symphony."




The Ruby Way(c) Solutions and Techniques in Ruby Programming
The Ruby Way, Second Edition: Solutions and Techniques in Ruby Programming (2nd Edition)
ISBN: 0672328844
EAN: 2147483647
Year: 2004
Pages: 269
Authors: Hal Fulton

Similar book on Amazon

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net