jmiller
jmiller

Reputation: 11

Unexpected regular expression behaviour

When running the following piece of code in 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.

However, when using the same pattern and string on rubular I get a match. The irregularities already start when using the above code with pattern:

/^\\begin/

and string \begin. This does not match using the above snippet, but it does on rubular.com

I'm using Ruby 1.9.1

Upvotes: 1

Views: 65

Answers (1)

Mark Byers
Mark Byers

Reputation: 838226

The problem is with your input string. You have a backspace character "\b". Try using a single-quoted string instead:

string = '\begin{theorem}[blaat] \label{thm:foo}'

Upvotes: 3

Related Questions