bash regrex operator =~

来源:互联网 发布:xnview mac 破解 编辑:程序博客网 时间:2024/06/04 19:10

If you're using a recent bash (v3+) suggest bash regex comparison operator =~, i.e.

if [[ "$HOST" =~ ^user.* ]]; then    echo "yes"
 fi

To match this or that in a regex use |, i.e.

if [[ "$HOST" =~ ^user.*|^host1 ]]; then    echo "yes"fi
----------

The =~ Regular Expression matching operator within a double brackets test expression. (Perl has a similar operator.)

#!/bin/bashvariable="This is a fine mess."echo "$variable"# Regex matching with =~ operator within [[ double brackets ]].if [[ "$variable" =~ T.........fin*es* ]]# NOTE: As of version 3.2 of Bash, expression to match no longer quoted.then  echo "match found"      # match foundfi

Or, more usefully:

#!/bin/bashinput=$1if [[ "$input" =~ "[0-9][0-9][0-9]-[0-9][0-9]-[0-9][0-9][0-9][0-9]" ]]#                 ^ NOTE: Quoting not necessary, as of version 3.2 of Bash.# NNN-NN-NNNN (where each N is a digit).then  echo "Social Security number."  # Process SSN.else  echo "Not a Social Security number!"  # Or, ask for corrected input.fi

refer the link
http://tldp.org/LDP/abs/html/bashver3.html#REGEXMATCHREF
0 0
原创粉丝点击