Ruby 中的正则表达式负向后查找似乎不起作用
制作一个参数解析器。我想将一个字符串拆分为一个数组,其中分隔符为 ", "
,除非前面有 "|"
。这意味着字符串
"foo, ba|, r, arg"
应该导致
`["foo", "ba|, r", "arg"]`
我尝试使用此正则表达式: (? ,它适用于 http://regexhero.net/tester/ 但是当我尝试
args.split(/(?<!\|), /)
使用 ruby 时,出现错误: undefined (?...) sequence: /(?
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: /(?<!\|), /
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
Ruby 的正则表达式引擎尚不支持lookbehind。
您需要切换到 1.9 或使用 Oniguruma。
如果这不是一个选项,您可以搜索
|,
并将其替换为某种标记。一切都说完了,把|,
放回去。您还可以尝试使用如下正则表达式:
但显然
(?:[^|])
不是零宽度,这意味着您之后需要做一些额外的工作。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.