[英]Unexpected regular expression behaviour
When running the following piece of code in ruby:在 ruby 运行下面这段代码时:
string = "\begin{theorem}[blaat] \label{thm:foo}"
pattern = /^\\begin\{(theorem|definition)\}(\[.+\])?\s\\label\{(thm|def):.+\}$/
if string =~ pattern then
puts "match"
else
puts "no match"
end
I get a "no match" output.我得到一个“不匹配”output。
However, when using the same pattern and string on rubular I get a match.但是,当在rubular上使用相同的模式和字符串时,我得到了匹配。 The irregularities already start when using the above code with pattern:将上述代码与模式一起使用时,违规行为已经开始:
/^\\begin/
and string \begin
.和字符串\begin
。 This does not match using the above snippet, but it does on rubular.com这与使用上面的代码片段不匹配,但它在 rubular.com 上匹配
I'm using Ruby 1.9.1我正在使用 Ruby 1.9.1
The problem is with your input string.问题出在您的输入字符串上。 You have a backspace character "\b"
.你有一个退格字符"\b"
。 Try using a single-quoted string instead:尝试改用单引号字符串:
string = '\begin{theorem}[blaat] \label{thm:foo}'
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.