开发者

Ruby Split By Part of a Regex

开发者 https://www.devze.com 2023-01-09 00:13 出处:网络
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

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?开发者_JAVA百科


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

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号