Bash regex z cudzysłowami?

Następujący kod

number=1
if [[ $number =~ [0-9] ]]
then
  echo matched
fi
Działa. Jeśli próbuję użyć cudzysłowów w regex, to jednak zatrzymuje się:
number=1
if [[ $number =~ "[0-9]" ]]
then
  echo matched
fi
Ja też próbowałem. Co przegapiłem?

Dość zabawne, bash advanced scripting guide sugeruje, że powinno to zadziałać.

Bash w wersji 3.2.39.

Author: codeforester, 2008-10-20

4 answers

Zmieniono między 3.1 A 3.2 . Guess the advanced guide needs a update.

To jest zwięzły opis nowego funkcje dodane do bash-3.2 od wydanie bash-3.1. Jak zawsze, strona podręcznika (doc / bash.1) jest miejscem szukać pełnych opisów.

  1. nowe funkcje w Bash

Snip

F. cytowanie argumentu string do [[command ' s = ~ operator now forces dopasowanie strun, jak z Pozostałe operatory pasujące do wzorców.

Niestety spowoduje to złamanie istniejącego cudzysłowu przy użyciu skryptów, chyba że masz wgląd w Przechowywanie wzorców w zmiennych i używanie ich zamiast wyrażeń regularnych bezpośrednio. Przykład poniżej.

$ 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
 102
Author: Vinko Vrsalovic,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2008-10-20 12:36:36

Bash 3.2 wprowadził opcję zgodności compat31, która przywraca zachowanie cytowania wyrażeń regularnych Basha do 3.1

Bez kompat31:

$ 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

Z 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

Link do patcha: http://ftp.gnu.org/gnu/bash/bash-3.2-patches/bash32-039

 17
Author: Nicholas Sushkin,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2011-06-27 19:00:09

GNU bash, wersja 4.2.25 (1)-release (x86_64-pc-linux-gnu)

Niektóre przykłady string match i regex match

    $ 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
 6
Author: abc,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2015-11-17 01:00:19

Jak wspomniano w innych odpowiedziach, umieszczenie wyrażenia regularnego w zmiennej jest ogólnym sposobem osiągnięcia zgodności z różnymi wersjami bash . Można również użyć tego obejścia, aby osiągnąć to samo, zachowując Wyrażenie regularne w wyrażeniu warunkowym:

$ number=1
$ if [[ $number =~ $(echo "[0-9]") ]]; then echo matched; fi
matched
$ 
 3
Author: Digital Trauma,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2014-02-13 18:34:43