天天看點

Bash字元串處理(與Java對照) - 22.判斷字元串是否數字串Bash字元串處理(與Java對照) - 22.判斷字元串是否數字串

Bash字元串處理(與Java對照) - 22.判斷字元串是否數字串

In Java

用正規表達式比對方式判斷字元串是否數字串

String str = "1234";

if (str.matches("\\d+")) {

    // matched, it's digit string

}

In Bash

使用模式比對(Pattern Matching)判斷字元串是否數字串

方法:[[ $STR != *[!0-9]* ]]

解讀:$STR != *[!0-9]* 表示不比對非數字串,反過來講就是隻比對數字串。

方法:[[ ! $STR == *[!0-9]* ]]

解讀:$STR == *[!0-9]* 表示隻要包含非數字字元就為真,前面加上!操作符,表示相反,也就是說隻有當全部是數字字元時才為真。

man bash 寫道 [[ expression ]]

              When  the  == and != operators are used, the string to the right

              of the operator is considered a pattern and matched according to

              the  rules  described  below under Pattern Matching.  The return

              value is 0 if the string matches or does not match the  pattern,

              respectively,  and  1 otherwise.  Any part of the pattern may be

              quoted to force it to be matched as a string.

[[email protected] root]# STR=12345

[[email protected] root]# [[ $STR != *[!0-9]* ]] && echo "yes"

yes

[[email protected] root]# [[ ! $STR == *[!0-9]* ]] && echo "yes"

yes

[[email protected] root]# STR=12345a

[[email protected] root]# [[ $STR != *[!0-9]* ]] && echo "yes"  

[[email protected] root]# [[ ! $STR == *[!0-9]* ]] && echo "yes"

[[email protected] root]# STR=abcd

[[email protected] root]# [[ $STR != *[!0-9]* ]] && echo "yes"  

[[email protected] root]# [[ ! $STR == *[!0-9]* ]] && echo "yes"

[[email protected] root]# 

使用正規表達式比對判斷字元串是否數字串(Bash 3.0)

格式:[[ $STR =~ ^[0-9]+$ ]]

格式:[[ $STR =~ ^[[:digit:]]+$ ]]

man bash 寫道 [[ expression ]]

              An  additional  binary  operator,  =~,  is available, with the same precedence as == and !=.  When it is

              used, the string to the right of the operator is considered an extended regular expression  and  matched

              accordingly (as in regex(3)).  The return value is 0 if the string matches the pattern, and 1 otherwise.

              If the regular expression is syntactically incorrect, the conditional expression’s return  value  is  2.

[[email protected] ~]# STR=12345

[[email protected] ~]# [[ $STR =~ ^[0-9]+$ ]] && echo "yes"

yes

[[email protected] ~]# [[ $STR =~ ^[[:digit:]]+$ ]] && echo "yes"

yes

[[email protected] ~]# STR=12345a

[[email protected] ~]# [[ $STR =~ ^[0-9]+$ ]] && echo "yes"      

[[email protected] ~]# [[ $STR =~ ^[[:digit:]]+$ ]] && echo "yes"

[[email protected] ~]# STR=abcd

[[email protected] ~]# [[ $STR =~ ^[0-9]+$ ]] && echo "yes"      

[[email protected] ~]# [[ $STR =~ ^[[:digit:]]+$ ]] && echo "yes"

[[email protected] ~]#

注意:Bash 3.0 以上才支援,下面是在 Bash 2.0 下執行的結果。

[[email protected] root]# STR=12345

[[email protected] root]# [[ $STR =~ ^[0-9]+$ ]] && echo "yes"

-bash: conditional binary operator expected

-bash: syntax error near `=~'

[[email protected] root]#

使用sed -n /re/p 來判斷字元串是否數字串

格式:[ "$(sed -n "/^[0-9]\+$/p" <<< "$STR")" ]

格式:[ "$(echo "$STR" | sed -n "/^[0-9]\+$/p")" ]

man sed 寫道        -n, --quiet, --silent

              suppress automatic printing of pattern space

       /regexp/

              Match lines matching the regular expression regexp.

       p      Print the current pattern space.

[[email protected] ~]# STR=12345

[[email protected] ~]# [ "$(sed -n "/^[0-9]\+$/p" <<< "$STR")" ] && echo "yes"

yes

[[email protected] ~]# [ "$(echo "$STR" | sed -n "/^[0-9]\+$/p")" ] && echo "yes"  

yes

[[email protected] ~]# STR=12345a

[[email protected] ~]# [ "$(sed -n "/^[0-9]\+$/p" <<< "$STR")" ] && echo "yes"      

[[email protected] ~]# [ "$(echo "$STR" | sed -n "/^[0-9]\+$/p")" ] && echo "yes"

[[email protected] ~]# STR=abcd

[[email protected] ~]# [ "$(sed -n "/^[0-9]\+$/p" <<< "$STR")" ] && echo "yes"   

[[email protected] ~]# [ "$(echo "$STR" | sed -n "/^[0-9]\+$/p")" ] && echo "yes"

[[email protected] ~]#

使用 grep/egrep 來判斷字元串是否數字串

格式:grep -q "^[0-9]\+$" <<< "$STR"

格式:egrep -q "^[0-9]+$" <<< "$STR"

man grep 寫道 Egrep is the same as grep -E.

       -E, --extended-regexp

              Interpret PATTERN as an extended regular expression (see below).

       -e PATTERN, --regexp=PATTERN

              Use PATTERN as the pattern; useful to protect patterns beginning with -.

       -q, --quiet, --silent

              Quiet;  do  not  write  anything  to standard output.  Exit immediately with zero status if any match is

              found, even if an error was detected.  Also see the -s or --no-messages option.

[[email protected] ~]# STR=123456

[[email protected] ~]# egrep "^[0-9]+$" <<< "$STR"

123456

[[email protected] ~]# grep "^[0-9]\+$" <<< "$STR"

123456

[[email protected] ~]#

本文連結:http://codingstandards.iteye.com/blog/1212725   (轉載請注明出處)

傳回目錄:Java程式員的Bash實用指南系列之字元串處理(目錄) 

上節内容:Bash字元串處理(與Java對照) - 21.字元串正則比對

下節内容:Bash字元串處理(與Java對照) - 23.字元串替換、子串删除、子串截取