开发者

Regex matching in if not working [duplicate]

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

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜