Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Regex slightly different in Ruby 2?

Tags:

regex

posix

ruby

I just ported a small gem from Ruby 1.9.3 to the spiffy new Ruby 2.0.0. The only change I had to make was in a regular expression.

Under 1.9.3, the following regex would match any string containing characters other than digits, number-related punctuation, and whitespace (including non-breaking space).

/[^[[:space:]]\d\-,\.]/

Under 2.0.0, I had to move the Posix space class away from the start of the negation class.

/[^\d\-,\.[[:space:]]]/

I haven't found this change mentioned in the patch notes I've reviewed. Is it documented anywhere?

like image 542
Daniel Ashton Avatar asked Feb 17 '23 09:02

Daniel Ashton


1 Answers

The regular expression engine has been changed to Onigmo (based on Oniguruma) and this might be causing issues.

As far as I can tell, you're declaring the regular expression incorrectly. The second set of brackets is not required:

/[^[:space:]\d\-,\.]/

The [:space:] declaration is only invalid inside of a set so you will see it appear as [[:space:]] if used in isolation. In your case you have several other additions to the set.

I'm not sure why \s would not have sufficed in this case.

like image 140
tadman Avatar answered Feb 20 '23 00:02

tadman