0

I'm unable to understand why the following pattern matches here:

$ [[ "abcdef12" == ab!(cd)* ]] && echo matched
matched

According to the man page (unless I've misunderstood something) it should not :

!(pattern-list)
Matches anything except one of the given patterns

How "cd" could be matched by "!(cd)" ?

(ksh version used : "AJM 93u+ 2012-08-01")

Gilles Pion
  • 166
  • 1
  • 8
  • Does it make a difference if you use the [...] form, instead of the [[...]] form ? – mao Dec 01 '22 at 10:09
  • since '[' ']' is only for upward compalibility purposes I'm pretty sure that it doesn't even support patterns BTW I've found a working method: – Gilles Pion Dec 02 '22 at 11:05
  • (continued) The following pattern matches any string starting with "ab" followed by anything except "xx" : ab@(!(xx)&@(??))* Not supported by orignal ksh93 apparently but works on relatively old versions, so I'm ok with that. – Gilles Pion Dec 02 '22 at 11:21
  • the [ ... ] form works for me, but is more verbose, needing `if ...then ...else...fi` , I prefer the brevity of the [[..]] form. (Version A 2020.0.0) – mao Dec 02 '22 at 14:13
  • @mao It's very strange that the "[ "" == ]" matches since between single squares brackets pattern matching is not supported. For instance: [[ a = a* ]] && print match => outputs "match" [ a = a* ] && print match => outputs nothing – Gilles Pion Dec 05 '22 at 13:10

1 Answers1

0

Understood !

since "!(cd)" matches also empty string, the pattern matches "abcdef12"

I'm now looking for the best way to match any string beginning with "ab" not followed by "cd" using only shell pattern (not regexps, it have to work with old ksh versions)

Gilles Pion
  • 166
  • 1
  • 8