views:

603

answers:

5

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...

A: 

In PCRE, you use a negative look-behind:

(:<!x)y

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

Daniel
+6  A: 

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.

jbourque
A: 

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

Blindy
+1  A: 

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)/
Jefromi
Ah, I'm not a Ruby expert - jbourque says negative lookbehind is in newer Ruby. There's your real answer.
Jefromi
[^x] must match one character. if y occurs at the beginning of the line we should match it: /(?:\A|^|[^x])y/
glenn jackman
A: 

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"]
Pesto