[英]Regex pattern not capturing the second occurrence of the match
我有字符串:
Error: heroku 6.14.38 is already installed
To upgrade to 7.7.1, run `brew upgrade heroku`
我正在做.slice(/([\\d+\\.]+)/)
,它给了我6.14.38
,但没有给我7.7.1
。
我怎么也可以得到它?
您可以使用扫描方法:
str = "Error: heroku 6.14.38 is already installed
To upgrade to 7.7.1, run `brew upgrade heroku`"
puts(str.scan(/((?:\d+\.)+\d+)/))
打印:
6.14.38
7.7.1
str = "Error: heroku 6.14.38 is already installed
To upgrade to 7.7.1, run `brew upgrade heroku`"
r = /
\d{1,2} # match one or two digits
\. # match decimal
\d{1,2} # match two digits
\. # match decimal
\d{1,2} # match two digits
.+? # match any number of characters, lazily
\K # discard match so far
\d{1,2} # match one or two digits
\. # match decimal
\d{1,2} # match two digits
\. # match decimal
\d{1,2} # match two digits
/xm # free-spacing regex definition and multiline modes
str[r]
#=> "7.7.1"
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.