gpt4 book ai didi

ruby - 解析 Ruby 中的字符串以获得多个值?

转载 作者:太空宇宙 更新时间:2023-11-03 17:41:36 25 4
gpt4 key购买 nike

如何解析以下字符串:

电话 + 300 分钟和无限短信 - 24 个月计划 $25

获取括号内的值,即

电话 + [300] 分钟和 [无限制] 短信 - [24] 个月计划 $[25]

最佳答案

看情况,如果都是这样,那么:

/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

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

关于ruby - 解析 Ruby 中的字符串以获得多个值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1991776/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com