开发者

Ruby: hexadecimal in regular expressions

开发者 https://www.devze.com 2023-03-19 07:09 出处:网络
I need to match an md5 checksum in a regular expression in a Ruby (actually Rails) program. I found out somewhere that I can match hexadecimal strings with \\h sequence, but I can\'t find the link any

I need to match an md5 checksum in a regular expression in a Ruby (actually Rails) program. I found out somewhere that I can match hexadecimal strings with \h sequence, but I can't find the link anymore.

I'm using that sequence and my code is working in Ruby 1.9.2. I can make it working even under plain IRB (so it's not a Rails extension).

ruby-1.9.2-p180 :007 > "123abcdf" =~ /^\h+$/; $~
 => #<MatchData "123abcdf"> 
ruby-1.9.2-p180 :008 > "123abcdfg" =~ /^\h+$/; $~
 => nil 

However my IDE mark that expression as wrong and I can't find any reference which cites that sequence.

Is t开发者_高级运维he \h sequence legal in Ruby Regex under any environment/version or should I trust my ide and replace it with something like [abcdef\d]?


Yes it is. Check the official doc for the complete documentation for regex in Ruby.

Note that \h will match uppercase letters too, so it's actually equivalent to [a-fA-F\d]


According to this \h is part of oniguruma, which I believe is standard in ruby 1.9.

0

精彩评论

暂无评论...
验证码 换一张
取 消