Bash – Extracting a string, according to a pattern, in a bash script

bashshell-scriptstring

In bash, suppose that I have a string strname:

strname="ph7go04325r"

I would like to extract the characters between the first "3" character and the last "r" character in strname, saving the result in a string strresult. In the example above, the resulting strresult would be:

strresult="25"

The first "3" character is not necessarily at string position 8 in strname; likewise, the last "r" is not necessarily at string position 11. Thus, both of the following strings strname should yield strresult="25":

strname="ph11go04325raa"
strname="325r"
strname="rgo04325raa"

Also, strname=ph12go04330raa" should yield strresult="30".

I am new to bash scripting, and I don't know where to begin to do string pattern matching like this. Do you have any suggestions?

Best Answer

You can use a regex in bash (3.0 or above) to accomplish this:

if [[ $strname =~ 3(.+)r ]]; then
    strresult=${BASH_REMATCH[1]}
else
    echo "unable to parse string $strname"
fi

In bash, capture groups from a regex are placed in the special array BASH_REMATCH. Element 0 contains the entire match, and 1 contains the the match for the first capture group.

Related Question