当前位置:  开发笔记 > 编程语言 > 正文

带引号的bash正则表达式?

如何解决《带引号的bash正则表达式?》经验,为你挑选了4个好方法。

以下代码

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.



1> Vinko Vrsalo..:

它在3.1和3.2之间变化.猜猜高级指南需要更新.

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

    Bash的新功能

SNIP

F.引用[[command's =〜运算符]的字符串参数现在强制字符串匹配,就像其他模式匹配运算符一样.

遗憾的是,这会破坏现有的使用脚本的引用,除非您有洞察力将模式存储在变量中并直接使用它们而不是正则表达式.以下示例.

$ 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


这真的很有趣.引用的正则表达式不再起作用.带空格的不带引号的正则表达式不起作用.基于变量的正则表达式即使包含空格也可以工作.真是一团糟.

2> Nicholas Sus..:

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



3> Ankur Agarwa..:

GNU bash,版本4.2.25(1)-release(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



4> Digital Trau..:

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

$ number=1
$ if [[ $number =~ $(echo "[0-9]") ]]; then echo matched; fi
matched
$ 

推荐阅读
mobiledu2402851173
这个屌丝很懒,什么也没留下!
DevBox开发工具箱 | 专业的在线开发工具网站    京公网安备 11010802040832号  |  京ICP备19059560号-6
Copyright © 1998 - 2020 DevBox.CN. All Rights Reserved devBox.cn 开发工具箱 版权所有