ubuntuusers.de

Regex mit Quadrator bei If

Status: Ungelöst | Ubuntu-Version: Xubuntu 18.04 (Bionic Beaver)
Antworten |

Fried-rich

Anmeldungsdatum:
2. Mai 2013

Beiträge: 1148

Halo,

ich will prüfen ob ein String mit "part1" oder "part01" usw. endet. Ich dachte, dass ein vorangestellten regulären Ausdruck mit + oder {1,} beliebig oft, aber min. einmal vorhanden sein muss. Das geht so in einem Shellscript aber nicht. Wenn der String

blablabla.part1

heißt und das Script

if [[ "$string" = part[0-9] ]] ; then
 echo "ja"
fi

wird ja ausgegeben. Heißt der String aber

blablabla.part01

und das Script

if [[ "$string" = part[0-9]+ ]] ; then
 echo "ja"
fi

geht das nicht. Auch nicht so

if [[ "$string" = part[0-9]{1,} ]] ; then

Wird das in der Shell anders umgesetzt?

Friedrich

sebix Team-Icon

Moderator, Webteam

Anmeldungsdatum:
14. April 2009

Beiträge: 5582

Was du da machst, ist kein regulaerer Ausdruck, sondern normale expansion

              [...]  Matches  any  one  of  the enclosed characters.  A pair of characters separated by a hyphen denotes a range expression; any character that falls between those two characters, inclusive, using the current locale's collating sequence and character set, is matched.  If the first character following the [ is a !  or a ^
                     then any character not enclosed is matched.  The sorting order of characters in range expressions is determined by the current locale and the values of the LC_COLLATE or LC_ALL shell variables, if set.  To obtain the traditional interpretation of range expressions, where [a-d] is equivalent to [abcd], set  value  of
                     the LC_ALL shell variable to C, or enable the globasciiranges shell option.  A - may be matched by including it as the first or last character in the set.  A ] may be matched by including it as the first character in the set.

Du kannst aber =~ verwenden, siehe manpage:

              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.  If the nocasematch shell option is enabled, the match is performed without regard to the case of alphabetic characters.  Any part of the pattern may be quoted to force the quoted portion to be matched as a string.   Bracket  expressions
              in  regular  expressions  must be treated carefully, since normal quoting characters lose their meanings between brackets.  If the pattern is stored in a shell variable, quoting the variable expansion forces the entire pattern to be matched as a string.  Substrings matched by parenthesized subexpressions within the regular
              expression are saved in the array variable BASH_REMATCH.  The element of BASH_REMATCH with index 0 is the portion of the string matching the entire regular expression.  The element of BASH_REMATCH with index n is the portion of the string matching the nth parenthesized subexpression.

Fried-rich

(Themenstarter)

Anmeldungsdatum:
2. Mai 2013

Beiträge: 1148

Ich hab =~ immer im Sinne von 'beinhaltet' verwendet, also wenn man prüfen will ob etwas in einem string enthalten ist. WS wäre wichtig, dass der zu prüfende string auch nur am Ende und nicht auch in der Mitte steht. Geht das damit, z. B. mit einem * davor aber nicht danach? Kann ich nicht prüfen, bin auf Arbeit.

Fried-rich

(Themenstarter)

Anmeldungsdatum:
2. Mai 2013

Beiträge: 1148

Hab es versucht, ist wie vermutet. Ich kann nicht unterscheiden ob der String nur am Ende steht oder nicht. Er behandelt den String

part001

genauso wie den String

part001blabla

Das sollte so aber nicht sein. Es soll geprüft werden ob da "part" mit einer beliebigen Folge von Zahlen zwischen 0 und 9 steht und nichts nach den Zahlen.

Ich hab das jetzt erstmal so gelöst

if [[ "$part" =~ part[0-9]{1,}^. ]] ; then

Ob das nun die hohe Kunst ist, k.A.

seahawk1986

Anmeldungsdatum:
27. Oktober 2006

Beiträge: 11250

Wohnort: München

$ matcht in Regulären Ausdrücken auf das Ende der Zeile bzw. des Strings:

$ [[ "part001" =~ part[0-9]{1,}$ ]] && echo "match" || echo "no match"
match
$ [[ "part001blabla" =~ part[0-9]{1,}$ ]] && echo "match" || echo "no match"
no match 

rklm Team-Icon

Projektleitung

Anmeldungsdatum:
16. Oktober 2011

Beiträge: 13177

Und hier die Old-School-Variante für POSIX-Fans (d.h. funktioniert auch in der sh):

1
2
3
if expr "$s" : '.*part[0-9]\+$' >/dev/null; then
  echo "Match: $s"
fi
Antworten |