re = /(?:^|(?<=[, ]))(?:next to|near|beside|opp).+?(?=,|$)/mi
str = 'Opp foobar, foocity" --> Must match "Opp foobar"
Fooplace, near barplace, barcity" --> Must match "near barplace"
Fooplace, Shoppers Stop, foocity"--> Must match nothing
Fooplace, opp barplace"--> Must match "opp barplace"
'
# Print the match result
str.scan(re) do |match|
puts match.to_s
end
Please keep in mind that these code samples are automatically generated and are not guaranteed to work. If you find any syntax errors, feel free to submit a bug report. For a full regex reference for Ruby, please visit: http://ruby-doc.org/core-2.2.0/Regexp.html