在Ruby中解析一个字符串以获取多个值?

1 ruby regex

我该如何解析以下字符串:

电话+ 300分钟和无限文本 - 24个月计划25美元

获得括号内的值,即

电话+ [300]分钟和[无限制]文本 - [24]月计划$ [25]

hrn*_*rnt 5

取决于,如果他们都看起来像那样,那么:

/Phone \+ (\w+) mins & (\w+) texts - (\d+) month plan \$(\w+)/

这假定计划可能包含无限分钟.

您可以像这样使用正则表达式:

str =  "Phone + 300 mins & unlimited texts - 24 month plan $25"
regex =  /Phone \+ (\w+) mins & (\w+) texts - (\d+) month plan \$(\w+)/
match = regex.match(str).to_a
Run Code Online (Sandbox Code Playgroud)

现在比赛是 ["Phone + 300 mins & unlimited texts - 24 month plan $25", "300", "unlimited", "24", "25"]