我们如何从rubular表达式中的字符串中提取单词?
$0Public$robotics00$india0$000facebook
如果我们想Public robotics india facebook从上面的字符串中提取单词,我们该怎么做?
我正在使用([^0$]),但它给出的字母不是正确的单词。
我们可以在这里尝试正则表达式拆分:
input = "$0Public$robotics00$india0$000facebook"
parts = input.split(/\d*\$\d*/)
puts parts
这打印:
Public
robotics
india
facebook
您可以匹配$后跟可选零,并使用捕获组来匹配除$ 0空格字符以外的字符
\$0*([^0$\s]+)
解释
\$匹配一个$字符0*匹配可选零(捕获组 1
[^0$\s]+匹配除0 $空格字符以外的任何字符的 1 次以上)关闭组 1re = /\$0*([^0$\s]+)/
str = '$0Public$robotics00$india0$000facebook
'
# Print the match result
str.scan(re) do |match|
puts match.first
end
输出
Public
robotics
india
facebook