Bash regex using BASH_REMATCH not capturing all groups

RESULT contains the literal characters | and *. These two characters have special meaning in regex’s so they need to be escaped. (NOTE: [ also has special meaning but OP has correctly escaped it.)

One modification to OP’s regex:

regex="([[:digit:]]+)\|([[:digit:]])\*;\[([[:digit:]]+);STOP;([[:digit:]]+);"
                     ^^             ^^
###########
# a shorter alternative:

regex="([0-9]+)\|([0-9])\*;\[([0-9]+);STOP;([0-9]+);"
               ^^       ^^

Taking for a test drive:

[[ "$RESULT" =~ $regex ]] && typeset -p BASH_REMATCH

declare -a BASH_REMATCH=([0]="1730624402|1*;[64452034;STOP;1730588408;" [1]="1730624402" [2]="1" [3]="64452034" [4]="1730588408")

NOTES:

  • OP hasn’t (yet) stated the desired contents of BASH_REMATCH[] so at this point I’m guessing this is the expected result
  • in this particular case I don’t see the need for the additional ? characters in the regex

Read more here: Source link