简体   繁体   中英

Trouble matching multiple patterns in Ruby (regex)

What is the difference between the floowing regexes: HEAD|GET , (HEAD|POST) & [HEAD|POST] ?

Basically, I want to extract the number after either HEAD or POST.

irb(main):001:0> "This is HEAD and a POST".match("HEAD|POST")
=> #<MatchData "HEAD">
irb(main):002:0> "This is HEAD and a POST".match("(HEAD|POST)")
=> #<MatchData "HEAD" 1:"HEAD">
irb(main):003:0> "This is HEAD and a POST".match("[HEAD|POST]")
=> #<MatchData "T">
irb(main):004:0> "This is HEAD 1 and a POST 2".match("[HEAD|POST] (.)")
=> #<MatchData "D 1" 1:"1">
irb(main):005:0>

The last regex didn't match the "2" that is after "POST". Why? Also, why is "D 1" being matched?

HEAD|POST and (HEAD|POST) match the same strings (either HEAD or POST); the second one captures the string while the first doesn't.

[HEAD|POST] matches a single character, any of ADEHOPST or |. So "This is HEAD and a POST".match("[HEAD|POST]") matches the single character T in This .

On the other hand, "This is HEAD 1 and a POST 2".match("[HEAD|POST] (.)") can't match the leading T because it isn't followed by a space - instead it matches the single D at the end of HEAD , plus the space and 1 following, capturing the 1.

try scan:

"This is HEAD 1 and a POST 2".scan /(HEAD|POST)\s(\d)/

=> [["HEAD", "1"], ["POST", "2"]]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM