pat='[^0-9]+([0-9]+)'
s='I am a string with some digits 1024'
[[ $s =~ $pat ]] # $pat must be unquoted
echo "${BASH_REMATCH[0]}"
echo "${BASH_REMATCH[1]}"
Output:
I am a string with some digits 1024
1024
Instead of assigning the regex to a variable ($pat
) we could also do:
[[ $s =~ [^0-9]+([0-9]+) ]]
Explanation
[[ $s =~ $pat ]]
construct performs the regex matching