Regex in KornShell

前端 未结 5 2083
忘掉有多难
忘掉有多难 2020-12-31 13:48

I am trying to check whether a variable is exactly two numbers but I can not seem to figure it out.

How do you do check regular expressions (regex) in KornShell (ksh

5条回答
  •  半阙折子戏
    2020-12-31 14:49

    Ksh has supported limited extended patterns since ksh88, using the

    special '(' pattern ')'
    

    syntax.

    In ksh88, the 'special' character prefixes change the number of matches expected:

    '*' for zero or more matches
    '+' at least one match
    '@' for exactly one match
    '?' for zero or one matches
    '!' for negation
    

    In ksh93, this was expanded with

    '{' min ',' max '}'
    

    to express an exact range:

    for w in 1423 12 "" abc 23423 9 33 3  333
    do
      [[ $w == {1,3}(\d) ]] && print $w has between 1 and three digits
      [[ $w == {2}(\d) ]] && print $w has exactly two digits
    done
    

    And finally, you can have perl-like clutter with '~', which introduces a whole new class of extensions,including full regular expressions with:

    '~(E)( regex )'

    More examples can be found in Finnbarr P. Murphy's blog

提交回复
热议问题