Skip to content

Instantly share code, notes, and snippets.

@phluid61
Last active February 1, 2016 05:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save phluid61/67953b6aeb01c82d0c21 to your computer and use it in GitHub Desktop.
Save phluid61/67953b6aeb01c82d0c21 to your computer and use it in GitHub Desktop.
Ruby / Oniguruma case-insensitive non-word quirk
$ ruby -v re.rb
ruby 2.3.0p0 (2015-12-25 revision 53290) [x86_64-linux]
non-word:
/\W/ => []
/\W/i => []
/[\W]/ => []
/[\W]/i => ["fF", "Ss"]
not word/leading word:
/^\w/ => ["f"]
/^\w/i => ["f"]
/[^\w]/ => []
/[^\w]/i => []
word:
/\w/ => ["f", "F", "f", "G", "S", "s", "o", "E", "k"]
/\w/i => ["f", "F", "f", "G", "S", "s", "o", "E", "k"]
/[\w]/ => ["f", "F", "f", "G", "S", "s", "o", "E", "k"]
/[\w]/i => ["f", "F", "f", "G", "S", "s", "o", "E", "k"]
not non-word/leading non-word:
/^\W/ => []
/^\W/i => []
/[^\W]/ => ["f", "F", "f", "G", "S", "s", "o", "E", "k"]
/[^\W]/i => ["f", "F", "f", "G", "S", "s", "o", "E", "k"]
$ jruby -v re.rb
jruby 9.0.4.0 (2.2.2) 2015-11-12 b9fb7aa OpenJDK 64-Bit Server VM 24.91-b01 on 1.7.0_91-b02 +jit [linux-amd64]
non-word:
/\W/ => []
/\W/i => []
/[\W]/ => []
/[\W]/i => ["fF", "S", "s", "k"]
not word/leading word:
/^\w/ => ["f"]
/^\w/i => ["f"]
/[^\w]/ => []
/[^\w]/i => []
word:
/\w/ => ["f", "F", "f", "G", "S", "s", "o", "E", "k"]
/\w/i => ["f", "F", "f", "G", "S", "s", "o", "E", "k"]
/[\w]/ => ["f", "F", "f", "G", "S", "s", "o", "E", "k"]
/[\w]/i => ["f", "F", "f", "G", "S", "s", "o", "E", "k"]
not non-word/leading non-word:
/^\W/ => []
/^\W/i => []
/[^\W]/ => ["f", "F", "f", "G", "S", "s", "o", "E", "k"]
/[^\W]/i => ["f", "F", "f", "G", "o", "E"]
$string = 'fFfGSsoEk'.freeze
def demo re, desc
puts "#{desc}:"
print " /#{re}/ => "; p $string.scan(/#{re}/)
print " /#{re}/i => "; p $string.scan(/#{re}/i)
print " /[#{re}]/ => "; p $string.scan(/[#{re}]/)
print " /[#{re}]/i => "; p $string.scan(/[#{re}]/i)
puts ''
end
demo('\W', 'non-word')
demo('^\w', 'not word/leading word')
demo('\w', 'word')
demo('^\W', 'not non-word/leading non-word')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment