2

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?

1 Answer 1

3

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

Sign up to request clarification or add additional context in comments.

2 Comments

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!
As far as I know, Rubular uses the Ruby 1.8 regex engine, and that doesn't know lookbehind yet.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.