您的位置:首页 > 运维架构 > Shell

bash还能使用正则表达式,以前都没留意过,都是用外部程序 sed, grep来完成的

2012-07-15 11:57 555 查看
 

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."


 
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: