简体   繁体   English

Ruby正则表达式“直到空行”

[英]Ruby regular expression to “go until blank line”

I have the following test string: 我有以下测试字符串:

puts "Wrong guess receives feedback"
p (game.guess(11) == "Too high!")

puts "Wrong guess deducts from remaining guesses"
p (game.remaining_guesses == 3)

In english I'm trying to: 我正在用英语尝试:

capture everything after "puts " 
until you get to a blank line
(aka beginning of a string followed immediately by end of string)

I know how to "go until you run into something". 我知道如何“直到遇到某事”。 Like for example, "go until you run into a double quote" 例如,“直到遇到双引号”

[6] pry(main)> re = /[^"]+/
=> /[^"]+/
[7] pry(main)> "stuff before a quote\"".slice(re)
=> "stuff before a quote"

I think re = /^$/ will "capture a blank line" http://rubular.com/r/E5F7wH6sNq 我认为re = /^$/将“捕获空白行” http://rubular.com/r/E5F7wH6sNq

So How would I go about capturing "everything after 'puts ' and until you get to a blank line"? 那么我将如何捕获“'put'之后的所有内容,直到您到达空白行”呢? I've tried the following: 我尝试了以下方法:

re = /puts ([^^$]+)/

But it didn't work: http://rubular.com/r/yUhA090fLm 但这没有用: http : //rubular.com/r/yUhA090fLm

This would be one way to do it: 这将是一种方法:

re = /puts (.*?)\r?\n\r?\n/m

.* means "zero or more of any character except newlines". .*表示“除换行符外,任何字符都零个或多个”。 I'll explain the ? 我会解释? later. 后来。

The m (for "multiline") modifier at the end makes it mean "zero or more of any character including newlines". 最后的m (对于“多行”)修饰符表示“零个或多个任何字符,包括换行符”。

The \\r?\\n\\r?\\n is two newlines in a row, accounting for the fact that it can be represented differently on different systems. \\r?\\n\\r?\\n连续是两个换行符,这说明在不同系统上可以用不同的方式表示它。 You could replace this part with $^ instead, but then the (.*?) will end up contain a trailing newline, because the $ for "end of line" matches after a newline, not before. 您可以用$^代替此部分,但是(.*?)最终将包含尾随换行符,因为“行尾”的$匹配换行符之后,而不是之前。

The ? ? makes .* "lazy" – it matches as little as possible, instead of being "greedy" and matching as much as possible. 使.* “ lazy” –尽可能少地匹配,而不是“贪婪”并尽可能多地匹配。 This is relevant if you have multiple blank lines: you presumably don't want it to greedily match "zero or more of any character until the very last blank line"; 如果您有多个空行,则这是相关的:您可能不希望它贪婪地匹配“零个或多个字符,直到最后一个空行”; you want it to lazily match "zero or more of any character until the first blank line". 您希望它延迟匹配“零个或多个任何字符,直到第一个空白行”。

One of the problems with your /puts ([^^$]+)/ is that [^abc] is effectively a shortcut for "any one character that isn't a, b or c". /puts ([^^$]+)/[^abc]实际上是“不是a,b或c的任何一个字符”的快捷方式。 It doesn't mean "not an 'a' followed by a 'b' followed by a 'c'". 这并不意味着“不是'a'跟着'b'跟着'c'”。

str.scan /(?<=puts ).*?(?=\R\R|\z)/m                                                                                                           
#⇒ [
#    [0] "\"Wrong guess receives feedback\"\np (game.guess(11) == \"Too high!\")",
#    [1] "\"Wrong guess deducts from remaining guesses\"\np (game.remaining_guesses == 3)"
# ]

Positive lookbehind puts , followed by non-greedy anything till two carriage returns / blank lines (note that \\R captures the latter on any platform , including Win and MacOS,) or the end of input. 正向后置puts ,然后是非贪婪的内容,直到出现两次回车符/空行(请注意\\R任何平台 (包括Win和MacOS)上捕获后者)或输入结束。

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

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