Why does BASH_REMATCH not work for a quoted regular expression?

前端 未结 3 1930
别那么骄傲
别那么骄傲 2020-11-30 06:51

The code is like this:

#!/bin/bash
if [[ foobarbletch =~ \'foo(bar)bl(.*)\' ]]
 then
     echo \"The regex matches!\"
     echo $BASH_REMATCH
     echo ${BAS         


        
3条回答
  •  慢半拍i
    慢半拍i (楼主)
    2020-11-30 07:06

    In your bash REGEX, you should remove quotes. That's why that doesn't work.

    If you have space, I recommend to use this way :

    #!/bin/bash
    x='foo bar bletch'
    if [[ $x =~ foo[[:space:]](bar)[[:space:]]bl(.*) ]]
    then
        echo The regex matches!
        echo $BASH_REMATCH      
        echo ${BASH_REMATCH[1]} 
        echo ${BASH_REMATCH[2]} 
    fi
    

提交回复
热议问题