Return a regex match in a Bash script, instead of replacing it

前端 未结 9 785
面向向阳花
面向向阳花 2021-01-31 04:30

I just want to match some text in a Bash script. I\'ve tried using sed but I can\'t seem to make it just output the match instead of replacing it with something.



        
9条回答
  •  星月不相逢
    2021-01-31 04:53

    You could do this purely in bash using the double square bracket [[ ]] test operator, which stores results in an array called BASH_REMATCH:

    [[ "TestT100String" =~ ([0-9]+) ]] && echo "${BASH_REMATCH[1]}"
    

提交回复
热议问题