How can I match spaces with a regexp in Bash?

前端 未结 3 1549
甜味超标
甜味超标 2020-12-03 13:15

I expect the code below to echo \"yes\", but it does not. For some reason it won\'t match the single quote. Why?

str=\"{templateUrl: \'}\"
regexp=\"templateU         


        
3条回答
  •  隐瞒了意图╮
    2020-12-03 14:06

    This should work:

    #!/bin/bash
    str="{templateUrl: '}"
    regexp="templateUrl:[[:space:]]*'"
    
    if [[ $str =~ $regexp ]]; then
      echo "yes"
    else
      echo "no"
    fi
    

    If you want to match zero or more whitespaces the * needs to added after [[:space:]].

提交回复
热议问题