Bash正则表达式在句子中查找特定单词 [英] Bash regex finding particular words in a sentence

查看:45
本文介绍了Bash正则表达式在句子中查找特定单词的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个这样的句子:

"The dog jumped over the moon because he likes jumping"

我想找到所有与 jump.* 匹配的单词,即 jumpedjumping.我该怎么做?

And I want to find all words that match jump.*, i.e. jumped and jumping. How can I do this?

目前我将句子放在一个变量中,$sentence.我知道我想测试的匹配词,例如$testjump.

Currently I have the sentence in a variable, $sentence. And I know the match word that I want to test against, e.g. $test is jump.

谢谢

推荐答案

试试这个正则表达式:

/\bjump.*?\b/

请参阅此处.\b 匹配单词边界和 jump.*? 之间以 jump 开头的所有内容.

See here. \b matches word boundaries and jump.*? everything between that starts with jump.

在 bash 中,您可以将它与 grep 一起使用:

In bash you can use it with grep:

echo $sentence | grep -oP "\b$test.*?\b"

这篇关于Bash正则表达式在句子中查找特定单词的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆