mudge / re2

Ruby bindings to RE2, a "fast, safe, thread-friendly alternative to backtracking regular expression engines like those used in PCRE, Perl, and Python".
http://mudge.name/re2/
BSD 3-Clause "New" or "Revised" License
129 stars 13 forks source link

Different behavior for '^' #29

Closed sk- closed 9 years ago

sk- commented 9 years ago

In plain ruby we have:

Regexp.new('^a').match("\na")
 => #<MatchData "a">

whereas in re2:

RE2::Regexp.new('^a').match("\na")
 => false
mudge commented 9 years ago

This is due to a difference in the underlying Google re2 library's syntax which isn't 100% compatible with PCRE: in order for ^ to match the beginning of a line, you need to use the multi-line mode flag.

RE2::Regexp.new('(?m)^a').match("\na")
=> true

Or, using a capturing group to return the match:

RE2::Regexp.new('(?m)(^a)').match("\na")
=> #<RE2::MatchData "a" 1:"a">
sk- commented 9 years ago

Could you add a section to the Readme stating the differences and incompatibilities of re2 compared to ruby's Regexp.