http://wiki.bash-hackers.org/syntax/ccmd/conditional_expression
Regular Expression Matching
Using the operator =~, the left hand side operand is matched against the extended regular expression (ERE) on the right hand side.
This is consistent with matching against patterns: Every quoted part of the regular expression is taken literally, even if it contains regular expression special characters.
Best practise is to put the regular expression to match against into a variable. This is to avoid shell parsing errors on otherwise valid regular expressions.
REGEX="^[[:upper:]]{2}[[:lower:]]*$"
# Test 1
STRING=Hello
if [[ $STRING =~ $REGEX ]]; then
echo "Match."
else
echo "No match."
fi
# ==> "No match."
本文介绍如何在Bash中使用=~操作符进行正则表达式匹配,并通过实例演示了如何将正则表达式存储在变量中以避免shell解析错误。

被折叠的 条评论
为什么被折叠?



