Ruby中的正则表达式负向后退似乎不起作用 [英] 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"
应该导致
`["foo", "ba|, r", "arg"]`
我正在尝试使用此正则表达式:(?<!\|),
,可在 http://regexhero.net/tester/中使用但是当我尝试
I'm trying to use this regex: (?<!\|),
which works in http://regexhero.net/tester/ but when I try
args.split(/(?<!\|), /)
在红宝石中,我得到一个错误:undefined (?...) sequence: /(?<!\|), /
in ruby, I get an error: undefined (?...) sequence: /(?<!\|), /
推荐答案
Ruby的regex引擎不支持lookbehind(尚未).
Ruby's regex engine doesn't support lookbehind (yet).
您需要切换到1.9或使用 Oniguruma .
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.
这篇关于Ruby中的正则表达式负向后退似乎不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!