开发者

Regex matching in if not working [duplicate]

开发者 https://www.devze.com 2023-03-24 07:30 出处:网络
This question already has answers here: 开发者_JAVA技巧 Closed 11 years ago. Possible Duplicate: How do I use regular expressions in bash scripts?
This question already has answers here: 开发者_JAVA技巧 Closed 11 years ago.

Possible Duplicate:

How do I use regular expressions in bash scripts?

Why isn't this working?

if [[ "foo" =~ "[f][o][o]" || "foo" =~ "(foo)" || "foo" =~ ".*" ]]
then
    echo "Success"
else
   echo "Fail"
fi
# Result: Fail
# Expected: Success

if [[ "foo" =~ "foo" ]]
then
    echo "Success"
else
   echo "Fail"
fi
# Result: Success
# Expected: Success


Remove the quotes from each regex:

if [[ "foo" =~ [f][o][o] || "foo" =~ (foo) || "foo" =~ .* ]]
then
  echo "Success"
else
  echo "Fail"
fi

Apparently, the quotes force the regex to be interpreted as a literal string.

0

精彩评论

暂无评论...
验证码 换一张
取 消