除非用 \ 转义,否则匹配的 Ruby 正则表达式
要识别以下内容:
[stack][overflow]
or
[is great!]
我做:
/\[([^\]]+)\](\[([^\]]+)\])?/.match(s)
现在我想扩展它,以便如果 [...][...]
或 [...]
出现在 < code>\ 它不会被识别:
\[stack][overflow] # => Should not match
\[is great!] # => Should not match
a \[b][c] \[d] [e] # => Should match [e]
我如何扩展正则表达式来实现此目的?
它应该可以在 Ruby 1.9.2 和 Ruby 1.8.7 中运行。
To identify things like:
[stack][overflow]
or
[is great!]
I do:
/\[([^\]]+)\](\[([^\]]+)\])?/.match(s)
Now I want to extend this such that if [...][...]
or [...]
comes after \
it won't be recognized:
\[stack][overflow] # => Should not match
\[is great!] # => Should not match
a \[b][c] \[d] [e] # => Should match [e]
How could I extend the regex to achieve this ?
It should work both in Ruby 1.9.2 and Ruby 1.8.7.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我想出了这个正则表达式:
您可以在这里尝试:
http://rubular.com/r/L796M2pcG7
希望有帮助。
编辑:对您的评论做出反应
编辑:下一篇
I came up with this regexp:
You can try it here:
http://rubular.com/r/L796M2pcG7
Hope that helps.
EDIT: reacted to your comment
EDIT: next one
您可以这样做:
(added
(^|[^\\])(\\\\)*
)这通过匹配主题字符串的开头(如果之前没有任何内容)来工作
[
),或除\
之外的任何字符,后跟任意数量的转义\\
。在这里尝试一下:http://rubular.com/r/ST9qOVjUXe
You can do this instead:
(added
(^|[^\\])(\\\\)*
)This works by matching either the start of the subject string (in case there is nothing before
[
), or any character except\
, followed by any number of escaped\\
.Try it here: http://rubular.com/r/ST9qOVjUXe