我有这样一句话:
"The dog jumped over the moon because he likes jumping"
我想找到所有匹配 jump.*
的单词,即 jumped
和 jumping
。我该怎么做?
目前我有一个变量中的句子,$sentence
。而且我知道我要测试的匹配词,例如$test
是跳转
。
谢谢
最佳答案
无管道的 Bash 解决方案
如果您想纯粹在 Bash 中执行此操作,您可以使用正则表达式匹配运算符和内置的 BASH_REMATCH 变量来保存结果。例如:
re='\bjump[[:alpha:]]*\b'
string="The dog jumped over the moon because he likes jumping"
for word in $string; do
[[ "$word" =~ $re ]] && echo "${BASH_REMATCH}"
done
根据您的语料库,这会正确返回以下结果:
jumped
jumping
关于regex - Bash 正则表达式在句子中查找特定单词,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11419885/