Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Regex negative lookbehind in Ruby doesn't seem to work

Making an argument parser. I want to split a string into an array where the delimiter is ", " except when preceded by "|". That means string

"foo, ba|, r, arg"

should result in

`["foo", "ba|, r", "arg"]`

I'm trying to use this regex: (?<!\|), which works in http://regexhero.net/tester/ but when I try

args.split(/(?<!\|), /)

in ruby, I get an error: undefined (?...) sequence: /(?<!\|), /

like image 688
tybro0103 Avatar asked Sep 30 '11 03:09

tybro0103


1 Answers

Ruby's regex engine doesn't support lookbehind (yet).

You'd need to switch to 1.9 or use Oniguruma.


If that's not an option, you can search for |, and replace it with some sort of marker. After all is said and done, put the |, back.

You can also try a regex like:

/(?:[^|]), /

But obviously the (?:[^|]) is not zero-width, which means you'll need to do some extra work afterwards.

like image 85
NullUserException Avatar answered Oct 02 '22 03:10

NullUserException