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]* 表示只要包含非数字字符就为真,前面加上!操作符,表示相反,也就是说只有当全部是数字字符时才为真。
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.
[root@smsgw root]# STR=12345
[root@smsgw root]# [[ $STR != *[!0-9]* ]] && echo "yes"
yes
[root@smsgw root]# [[ ! $STR == *[!0-9]* ]] && echo "yes"
yes
[root@smsgw root]# STR=12345a
[root@smsgw root]# [[ $STR != *[!0-9]* ]] && echo "yes"
[root@smsgw root]# [[ ! $STR == *[!0-9]* ]] && echo "yes"
[root@smsgw root]# STR=abcd
[root@smsgw root]# [[ $STR != *[!0-9]* ]] && echo "yes"
[root@smsgw root]# [[ ! $STR == *[!0-9]* ]] && echo "yes"
[root@smsgw root]#
使用正则表达式匹配判断字符串是否数字串(Bash 3.0)
格式:[[ $STR =~ ^[0-9]+$ ]]
格式:[[ $STR =~ ^[[:digit:]]+$ ]]
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.
[root@jfht ~]# STR=12345
[root@jfht ~]# [[ $STR =~ ^[0-9]+$ ]] && echo "yes"
yes
[root@jfht ~]# [[ $STR =~ ^[[:digit:]]+$ ]] && echo "yes"
yes
[root@jfht ~]# STR=12345a
[root@jfht ~]# [[ $STR =~ ^[0-9]+$ ]] && echo "yes"
[root@jfht ~]# [[ $STR =~ ^[[:digit:]]+$ ]] && echo "yes"
[root@jfht ~]# STR=abcd
[root@jfht ~]# [[ $STR =~ ^[0-9]+$ ]] && echo "yes"
[root@jfht ~]# [[ $STR =~ ^[[:digit:]]+$ ]] && echo "yes"
[root@jfht ~]#
注意:Bash 3.0 以上才支持,下面是在 Bash 2.0 下执行的结果。
[root@smsgw root]# STR=12345
[root@smsgw root]# [[ $STR =~ ^[0-9]+$ ]] && echo "yes"
-bash: conditional binary operator expected
-bash: syntax error near `=~'
[root@smsgw root]#
使用sed -n /re/p 来判断字符串是否数字串
格式:[ "$(sed -n "/^[0-9]\+$/p" <<< "$STR")" ]
格式:[ "$(echo "$STR" | sed -n "/^[0-9]\+$/p")" ]
suppress automatic printing of pattern space
/regexp/
Match lines matching the regular expression regexp.
p Print the current pattern space.
[root@jfht ~]# STR=12345
[root@jfht ~]# [ "$(sed -n "/^[0-9]\+$/p" <<< "$STR")" ] && echo "yes"
yes
[root@jfht ~]# [ "$(echo "$STR" | sed -n "/^[0-9]\+$/p")" ] && echo "yes"
yes
[root@jfht ~]# STR=12345a
[root@jfht ~]# [ "$(sed -n "/^[0-9]\+$/p" <<< "$STR")" ] && echo "yes"
[root@jfht ~]# [ "$(echo "$STR" | sed -n "/^[0-9]\+$/p")" ] && echo "yes"
[root@jfht ~]# STR=abcd
[root@jfht ~]# [ "$(sed -n "/^[0-9]\+$/p" <<< "$STR")" ] && echo "yes"
[root@jfht ~]# [ "$(echo "$STR" | sed -n "/^[0-9]\+$/p")" ] && echo "yes"
[root@jfht ~]#
使用 grep/egrep 来判断字符串是否数字串
格式:grep -q "^[0-9]\+$" <<< "$STR"
格式:egrep -q "^[0-9]+$" <<< "$STR"
-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.
[root@jfht ~]# STR=123456
[root@jfht ~]# egrep "^[0-9]+$" <<< "$STR"
123456
[root@jfht ~]# grep "^[0-9]\+$" <<< "$STR"
123456
[root@jfht ~]#
本文链接:http://codingstandards.iteye.com/blog/1212725 (转载请注明出处)
返回目录:Java程序员的Bash实用指南系列之字符串处理(目录)