天天看点

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.字符串替换、子串删除、子串截取