我有一根绳子如下。

"Included in PTA fees" or "Included in PTA fees 33$" or "Included in PTA fees $60"

我需要将此字符串与搜索PTA费用/费用以及是否有任何费用(数字)也附加到它匹配。
需要查找“PTA费用”以及任何给定字符串中的费用值,并返回true。
所以它应该是真的
"Included in PTA fees 33$" and
"Included in PTA fees $60"

对于"Included in PTA fees"
我试过使用下面的正则表达式。但它又是真的
"Included in PTA fees"

/#{PTA_FEE[0-9]}/i.match?(value)

最佳答案

您只需要将\S+作为regex的结束部分:

str =~ /Included in PTA fees? \S+/

\S+一个或多个非空白字符
?可选匹配
或者更准确地说:
str =~ /Included in PTA fees? (?:\d+\$|\$\d+)/

Live demo

09-11 20:23