Question

Using Ruby, how can I use a single regex to match all occurrences of 'y' in "xy y ay xy +y" that are NOT preceded by x (y, ay, +y)?
/[^x]y/ matches the preceding character too, so I need an alternative...

Was it helpful?

Solution

You need a zero-width negative look-behind assertion. Try /(?<!x)y/ which says exactly what you're looking for, i.e. find all 'y' not preceeded by 'x', but doesn't include the prior character, which is the zero-width part.

Edited to add: Apparently this is only supported from Ruby 1.9 and up.

OTHER TIPS

Negative look-behind is not supported until Ruby 1.9, but you can do something similar using scan:

"xy y ay xy +y".scan(/([^x])(y)/) # => [[" ", "y"], ["a", "y"], ["+", "y"]]
"xy y ay xy +y".scan(/([^x])(y)/).map {|match| match[1]}  # => ["y", "y", "y"]

Of course, this is much more difficult if you want to avoid much more than a single character before the y. Then you'd have to do something like:

"abby y crabby bally +y".scan(/(.*?)(y)/).reject {|str| str[0] =~ /ab/}  # => [[" ", "y"], [" ball", "y"], [" +", "y"]]
"abby y crabby bally +y".scan(/(.*?)(y)/).reject {|str| str[0] =~ /ab/}.map {|match| match[1]}  # => ["y", "y" "y"]

Ruby unfortunately doesn't support negative lookbehind, so you'll have trouble if you need to look for more than a single character. For just one character, you can take care of this by capturing the match:

/[^x](y)/

In PCRE, you use a negative look-behind:

(:<!x)y

Not sure if this is supported by Ruby, but you can always look up.

It can be done with negative look behind, (?<!x)y

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top