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

 

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还能使用正则表达式,以前都没留意过,都是用外部程序 sed, grep来完成的)