[Top][All Lists]

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: matching !(patterns)

From: Paul Jarc
Subject: Re: matching !(patterns)
Date: Tue, 29 Jan 2008 18:48:01 -0500
User-agent: Gnus/5.11 (Gnus v5.11) Emacs/22.1 (gnu/linux)

Linda Walsh <address@hidden> wrote:
> The longest matching substring (because you use "/" to start the pattern,
> yes?)

No, !() normally uses the longest match, just like *, *(), and +().

>    s="thomas rich george"
> Manpage says "!()" will match anything except one of the patterns.
>   "$s" is 1 pattern, no?  (no alternation); 1 pattern containing
> "thomas rich george", yes?


> Then if I use !($s) as the replacement pattern within in the substitute
> operator, ( ${s//!($s)/X} ) the !($s) should match any string
> except what is in "$s" ("thomas rich george").

It will match the longest substring of $s that is not $s itself -
which is the first N-1 characters of s.  With //, it will replace
every match, so the final character will also be replaced, since it is
not the same as all of $s.

> So in the substitute, the string to replace (the "!($s) part) should
> match nothing in my variable "$s", so I'd think no replacement would
> be done.

You're right that $s as a whole does not match !($s).  But that isn't
the only candidate for matching.  Every substring is a candidate.  The
first position where a match is found is used, with the longest match
starting at that position.

To check whether a string as a whole matches a pattern, instead of
checking for a matching substring, you can use case.  (Or, if the
pattern is a literal string with no special characters, you can use

>> echo \"${s//!($s)/X}\"
> "XX"                       # why two X's? if I use 1 "/" instead of double:
>> echo \"${s/!($s)/X}\"
> "Xe"                     # why an "e" afterwards?

With / instead of //, only the first match is replaced.


reply via email to

[Prev in Thread] Current Thread [Next in Thread]