I have a sentence like this:
"The dog jumped over the moon because he likes jumping"
And I want to find all words that match jump.*
, i.e. jumped
and jumping
. How can I do this?
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
.
Thank you
If you want to do this purely in Bash, you can use the regular expression matching operator and the built-in BASH_REMATCH variable to hold the results. For example:
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
Given your corpus, this correctly returns the following results:
jumped
jumping
Try this regex:
/\bjump.*?\b/
See here. \b
matches word boundaries and jump.*?
everything between that starts with jump
.
In bash you can use it with grep:
echo $sentence | grep -oP "\b$test.*?\b"
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With