shell script: longest match from right?

Return the position of matched string from right, awk match can do from left only.

e.g return pos 7 for search string "service" from "AA-service"
or return the matched string "service", then caculate the string length.

Thanks!.

It's easy enough to calculate the right offset from the left offset and string length.

string=$1
pattern=$2
left=${string%%$pattern}
echo $(( ${#left} + 1 ))

I think you need

echo $(( ${#string} - ${#left} ))

to meet OP requirement.