简体   繁体   English

在Ruby中匹配多个模式时遇到麻烦(正则表达式)

[英]Trouble matching multiple patterns in Ruby (regex)

What is the difference between the floowing regexes: HEAD|GET , (HEAD|POST) & [HEAD|POST] ? 浮动正则表达式HEAD|GET(HEAD|POST)[HEAD|POST]什么区别?

Basically, I want to extract the number after either HEAD or POST. 基本上,我想提取HEAD或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". 最后一个正则表达式与“ POST”后的“ 2”不匹配。 Why? 为什么? Also, why is "D 1" being matched? 另外,为什么匹配“ D 1”?

HEAD|POST and (HEAD|POST) match the same strings (either HEAD or POST); HEAD|POST(HEAD|POST)匹配相同的字符串(HEAD或POST); the second one captures the string while the first doesn't. 第二个捕获字符串,而第一个不捕获。

[HEAD|POST] matches a single character, any of ADEHOPST or |. [HEAD|POST]匹配单个字符,ADEHOPST或|中的任何一个。 So "This is HEAD and a POST".match("[HEAD|POST]") matches the single character T in This . 因此, "This is HEAD and a POST".match("[HEAD|POST]")匹配This中的单个字符T

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. 另一方面, "This is HEAD 1 and a POST 2".match("[HEAD|POST] (.)")无法匹配前导T因为它后面没有空格-而是匹配前导T HEAD末尾的单个D加上空格和1 ,捕获了1。

try scan: 尝试扫描:

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

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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