以下代码
number=1
if [[ $number =~ [0-9] ]]
then
echo matched
fi
作品。但是,如果我尝试在正则表达式中使用引号,它将停止:
number=1
if [[ $number =~ "[0-9]" ]]
then
echo matched
fi
我也尝试"\[0-9\]"
过。我想念什么?
有趣的是,bash高级脚本指南建议这应该可行。
Bash版本3.2.39。
以下代码
number=1
if [[ $number =~ [0-9] ]]
then
echo matched
fi
作品。但是,如果我尝试在正则表达式中使用引号,它将停止:
number=1
if [[ $number =~ "[0-9]" ]]
then
echo matched
fi
我也尝试"\[0-9\]"
过。我想念什么?
有趣的是,bash高级脚本指南建议这应该可行。
Bash版本3.2.39。
Answers:
在3.1和3.2之间进行了更改。猜猜高级指南需要更新。
这是对自bash-3.1发行以来添加到bash-3.2的新功能的简短描述。与往常一样,手册页(doc / bash.1)是查找完整描述的地方。
- Bash的新功能
剪断
F。现在将字符串参数用[[命令的=〜运算符引用,就像其他模式匹配运算符一样。
不幸的是,除非您有足够的能力将模式存储在变量中并直接使用它们而不是正则表达式,否则这将使用脚本破坏现有的报价。下面的例子。
$ bash --version
GNU bash, version 3.2.39(1)-release (i486-pc-linux-gnu)
Copyright (C) 2007 Free Software Foundation, Inc.
$ number=2
$ if [[ $number =~ "[0-9]" ]]; then echo match; fi
$ if [[ $number =~ [0-9] ]]; then echo match; fi
match
$ re="[0-9]"
$ if [[ $number =~ $re ]]; then echo MATCH; fi
MATCH
$ bash --version
GNU bash, version 3.00.0(1)-release (i586-suse-linux)
Copyright (C) 2004 Free Software Foundation, Inc.
$ number=2
$ if [[ $number =~ "[0-9]" ]]; then echo match; fi
match
$ if [[ "$number" =~ [0-9] ]]; then echo match; fi
match
Bash 3.2引入了兼容性选项compat31,该选项将bash正则表达式的引用行为恢复为3.1
没有compat31:
$ shopt -u compat31
$ shopt compat31
compat31 off
$ set -x
$ if [[ "9" =~ "[0-9]" ]]; then echo match; else echo no match; fi
+ [[ 9 =~ \[0-9] ]]
+ echo no match
no match
使用compat31:
$ shopt -s compat31
+ shopt -s compat31
$ if [[ "9" =~ "[0-9]" ]]; then echo match; else echo no match; fi
+ [[ 9 =~ [0-9] ]]
+ echo match
match
修补程序链接:http : //ftp.gnu.org/gnu/bash/bash-3.2-patches/bash32-039
GNU bash版本4.2.25(1)-发行版(x86_64-pc-linux-gnu)
字符串匹配和正则表达式匹配的一些示例
$ if [[ 234 =~ "[0-9]" ]]; then echo matches; fi # string match
$
$ if [[ 234 =~ [0-9] ]]; then echo matches; fi # regex natch
matches
$ var="[0-9]"
$ if [[ 234 =~ $var ]]; then echo matches; fi # regex match
matches
$ if [[ 234 =~ "$var" ]]; then echo matches; fi # string match after substituting $var as [0-9]
$ if [[ 'rss$var919' =~ "$var" ]]; then echo matches; fi # string match after substituting $var as [0-9]
$ if [[ 'rss$var919' =~ $var ]]; then echo matches; fi # regex match after substituting $var as [0-9]
matches
$ if [[ "rss\$var919" =~ "$var" ]]; then echo matches; fi # string match won't work
$ if [[ "rss\\$var919" =~ "$var" ]]; then echo matches; fi # string match won't work
$ if [[ "rss'$var'""919" =~ "$var" ]]; then echo matches; fi # $var is substituted on LHS & RHS and then string match happens
matches
$ if [[ 'rss$var919' =~ "\$var" ]]; then echo matches; fi # string match !
matches
$ if [[ 'rss$var919' =~ "$var" ]]; then echo matches; fi # string match failed
$
$ if [[ 'rss$var919' =~ '$var' ]]; then echo matches; fi # string match
matches
$ echo $var
[0-9]
$
$ if [[ abc123def =~ "[0-9]" ]]; then echo matches; fi
$ if [[ abc123def =~ [0-9] ]]; then echo matches; fi
matches
$ if [[ 'rss$var919' =~ '$var' ]]; then echo matches; fi # string match due to single quotes on RHS $var matches $var
matches
$ if [[ 'rss$var919' =~ $var ]]; then echo matches; fi # Regex match
matches
$ if [[ 'rss$var' =~ $var ]]; then echo matches; fi # Above e.g. really is regex match and not string match
$
$ if [[ 'rss$var919[0-9]' =~ "$var" ]]; then echo matches; fi # string match RHS substituted and then matched
matches
$ if [[ 'rss$var919' =~ "'$var'" ]]; then echo matches; fi # trying to string match '$var' fails
$ if [[ '$var' =~ "'$var'" ]]; then echo matches; fi # string match still fails as single quotes are omitted on RHS
$ if [[ \'$var\' =~ "'$var'" ]]; then echo matches; fi # this string match works as single quotes are included now on RHS
matches