views:

39

answers:

1

I have to write an argument parser, it is passed in as a string ala {a, b, c} delimiter is comma with a space after it, but it can be escaped with a pipe "|" before it. So I was thinking of writing a regex like /[^\|], / to split by, and then removing all the escape characters afterwards. But when using this it will split by the character before the delimiter as well.

How should I go about accomplishing what I'm looking for?

+2  A: 

If you're on version 1.9 of Ruby, you can split on /(?<!\|),/.

(?<!\|) is a negative lookbehind assertion meaning "ensure that the previous character is not a |".

Tim Pietzcker
Ah! I've been using Rubular to test my regex and it does not mention it at all so I never knew about it. Thanks!
Randuin
As far as I know, Rubular uses the Ruby 1.8 regex engine, and that doesn't know lookbehind yet.
Tim Pietzcker