Reg表达式:为什么“name”不作为匹配返回

时间:2012-10-12 01:26:41

标签: ruby regex

以下正则表达式将“Gray”和“James”作为匹配返回。我不明白为什么“名字”不匹配。你能解释一下吗?

"Name:  Gray, James"[/(\w+), (\w+)/]

2 个答案:

答案 0 :(得分:3)

以下是您的正则表达式如何工作的解释

Match the regular expression below and capture its match into backreference number 1 «(\w+)»
   Match a single character that is a “word character” (letters, digits, and underscores) «\w+»
      Between one and unlimited times, as many times as possible, giving back as needed (greedy) «+»
Match the characters “, ” literally «, »
Match the regular expression below and capture its match into backreference number 2 «(\w+)»
   Match a single character that is a “word character” (letters, digits, and underscores) «\w+»
      Between one and unlimited times, as many times as possible, giving back as needed (greedy) «+»
格雷,詹姆斯 (\ w +),(\ w +)

Name不会使用逗号。

答案 1 :(得分:0)

匹配逗号旁边的单词没有空格“(\ w +),”

匹配逗号和空格后的单词“,(\ w +)”