Bash can do regex extraction too by the way. Just get the string into a variable first, then run
[[ regex test on it. The
BASH_REMATCH array will hold a successful match and any parenthetical groupings.
Code:
string='this-program-2.4.13.jar'
re='-([[:digit:]]+\.[[:digit:]]+\.[[:digit:]]+)\.'
[[ "$string" =~ $re ]]
echo "${BASH_REMATCH[1]}"
It's best to store the regex in a separate variable, to avoid having to escape special characters. The one I chose above looks for combinations of
-number.number.number., and captures everything except the first dash and last period (the full match is stored in
${BASH_REMATCH[0]}, BTW). If any of the version numbers could have non-numeric characters then you'll have to change the character classes to follow suit, i.e. use
alnum instead of
digit.
You could even separate out the three subsections at the same time simply by adding a few more parentheses.
Code:
string='this-program-2.4.13.jar'
re='-(([[:digit:]]+)\.([[:digit:]]+)\.([[:digit:]]+))\.'
[[ "$string" =~ $re ]]
echo "${BASH_REMATCH[1]}"
echo "${BASH_REMATCH[2]}"
echo "${BASH_REMATCH[3]}"
echo "${BASH_REMATCH[4]}"