bash正则表达式带有引号?


77

以下代码

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。


4
获取和惠益分享是相当臭名昭著的,因为它提供了不正确(或者在更好的情况下,只是误导)的指导;可以将其视为Shell脚本的W3Schools。考虑将bash-hackers.org或wooledge Wiki作为维护准确性的替代方案。
查尔斯·达菲

Answers:


116

在3.1和3.2之间进行了更改。猜猜高级指南需要更新。

这是对自bash-3.1发行以来添加到bash-3.2的新功能的简短描述。与往常一样,手册页(doc / bash.1)是查找完整描述的地方。

  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

16
这真的很有趣。引用的正则表达式不再起作用。带空格的未引号正则表达式不起作用。基于变量的正则表达式即使包含空格也可以工作。真是一团糟。
帕维尔·西默达(PavelŠimerda)

21

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


7

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

5

如其他答案所述,将正则表达式放入变量中是实现不同对象之间兼容性的一种通用方法 版本。您还可以使用此替代方法来实现相同的目的,同时将正则表达式保留在条件表达式中:

$ number=1
$ if [[ $number =~ $(echo "[0-9]") ]]; then echo matched; fi
matched
$ 
By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.