[英]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.