Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ruby/20.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
匹配的Ruby正则表达式,除非使用\_Ruby_Regex_Lookbehind - Fatal编程技术网

匹配的Ruby正则表达式,除非使用\

匹配的Ruby正则表达式,除非使用\,ruby,regex,lookbehind,Ruby,Regex,Lookbehind,确定以下事项: [stack][overflow] or [is great!] 我有: 现在我想扩展它,如果[…][…]或[…]在\之后,它将无法被识别: \[stack][overflow] # => Should not match \[is great!] # => Should not match a \[b][c] \[d] [e] # => Should match [e] 我如何扩展正则表达式来实现这一点 它应该在Ruby 1.9.2

确定以下事项:

[stack][overflow]
or
[is great!]
我有:

现在我想扩展它,如果
[…][…]
[…]
\
之后,它将无法被识别:

\[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中都能工作。

您可以这样做:

/(^|[^\\\]])(\\\\)*(\[([^\]]+)\]){1,2}/
(添加了<代码>(^ |[^\\])(\\\\\\\)*)

这是通过匹配主题字符串的开头(如果
[
之前没有任何内容),或者匹配除
\
之外的任何字符,后跟任何数量的转义
\

[1]       => yes
[1][2]    => yes
x[1]      => yes
x[1][2]   => yes
\[1]      => no
\[1][2]   => no
\\[1]     => yes
\\[1][2]  => yes
\\\[1]    => no
\\\\[1]   => yes
\\\\\[1]  => no
\\\\\\[1] => yes    
...

在这里试试:

我想出了这个regexp:

/([^\\\[\]]|^)\[([^\]]+)\](\[([^\]]+)\])?/
您可以在这里尝试:

希望有帮助

编辑:对您的评论作出反应
编辑:下一个

你想不识别一个\后面的所有[…]吗?@Chris:只有一个或两个紧跟在``之后的
[…]
。我在这个问题上又添加了一个例子。
/(^\]]\[(^\]]+\](\[(^\]]+\])?/.match(\[a][b]
不应该匹配你的正则表达式。match(“\[a][b]”不应该匹配它匹配[b]只有我想如果你在
[^\\]
/([^\\\[\]]|^)\[([^\]]+)\](\[([^\]]+)\])?/