Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ruby regex: "capture string unless it is followed by..."

Tags:

regex

ruby

My regex captures quoted phrases:

"([^"]*)"

I want to improve it, by ignoring quotes, which are followed by ', -' (a comma, a space and a dash in this particular order).

How do I do this?

The test: http://rubular.com/r/xls6vN1w92

like image 483
krn Avatar asked Jan 26 '11 15:01

krn


1 Answers

This should do it, using a Negative Lookahead:

"(?!, -)([^"]*)"(?!, -)

A little icky, but it works. You want to make sure either quote isn't followed by your string, or else the match will start at the closing quotes.

http://rubular.com/r/yFMyUKJOHL

like image 55
Kobi Avatar answered Sep 22 '22 10:09

Kobi