简体   繁体   English

Ruby正则表达式在匹配包含\\ n个字符的字符串时返回意外结果

[英]Ruby regex returning unexpected results while matching a string containing \n characters

Below is the output from my irb console 以下是我的irb控制台的输出

2.3.2 :001 > str = "Whenever ThingA happens\nrepeats: Sunday, Monday and Tuesday\n\n\nDo ThingB"
 => "Whenever ThingA happens\nrepeats: Sunday, Monday and Tuesday\n\n\nDo ThingB"

2.3.2 :002 > str.match(/(.*)\s*(?:repeats)/)
 => #<MatchData "Whenever ThingA happens\nrepeats" 1:"Whenever ThingA happens">

2.3.2 :003 > str.match(/(.*)(?:repeats)/)
 => #<MatchData "repeats" 1:"">

Can anybody please help me understand what's going on with the 2nd regex /(.*)(?:repeats)/ which is causing it not return the expected result the 1st regex /(.*)\\s*(?:repeats)/ returns? 有人可以帮我了解第二个正则表达式/(.*)(?:repeats)/ ,这导致它没有返回预期的结果。第二个正则表达式/(.*)\\s*(?:repeats)/回报?

On rubular.com 2nd regex shows following match 1. Whenever ThingA happens\\n which is expected however Ruby's String#match returns unexpected results as shown in console output above. rubular.com上,第二个正则表达式显示以下匹配项1. Whenever ThingA happens\\n ,这都是预期的,但是Ruby的String#match返回意外的结果,如上面的控制台输出所示。

The dot does not match new line characters so it is selecting the character before repeats for the given line, which is nothing. 点与换行符不匹配,因此它是在重复给定行之前选择字符,这没什么。 Essentially the same as /(?=repeats)/ since repeats this the first word on this line. 基本上与/(?=repeats)/相同,因为在此行的第一个单词重复此单词。

Adding the m flag (make dot match newlines) will solve this for you 添加m标志(使点匹配换行符)将为您解决此问题

str.match(/(.*)repeats/m)
#=> #<MatchData "Whenever ThingA happens\nrepeats" 1:"Whenever ThingA happens\n">

The reason the first one works is becuase \\s indicates a whitespace character and that does match the new line \\n since new line is a whitespace character. 第一个起作用的原因是因为\\s表示空格字符,并且与新行\\n匹配,因为新行是空格字符。

The noncapture group has no impact as pointed out by @CarySwoveland @CarySwoveland指出,非捕获组没有影响

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

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