Zsh Mailing List Archive
Messages sorted by: Reverse Date, Date, Thread, Author

Re: [[ 'abcde' =~ (#i)Bcd ]]



On Tue, Nov 8, 2022 at 3:06 AM Ray Andrews <rayandrews@xxxxxxxxxxx> wrote:
>
>
> On 2022-11-07 13:50, Lawrence Velázquez wrote:
> > I concur with Roman. I doubt you actually need case-insensitive
> > regex.
> >
> I'm happy with what I've got working at the moment tho you guys would
> probably improve it.  Pardon my personal jargon but:
>
> local vvar=$( basename $cc[$aa] 2> /dev/null )

There is a zsh way for this:

  local var=${cc[$aa]:t}

"t" is short for tail. There is also "h" for head.

> if   [[ "$scope_msg" = 'BROAD' && $vvar = (#i)*$filter* ]]; then
> elif [[ "$scope_msg" = 'Case INsensitive TAME' && $vvar:u = $filter:u
> ]]; then
> elif [[ "$scope_msg" = 'Case Sensitive WILD' && $vvar =~ $filter ]]; then
> elif [[ "$scope_msg" = 'EXACT' && $vvar = $filter ]]; then
> else cc[$aa]=
> fi

Here WILD suggests a wildcard (a.k.a. glob, a.k.a. pattern) match, but
the code is doing a regex match. If your intention is to perform a
wildcard/glob/pattern match, do this:

    [[ $vvar == $~filter ]]

Or, if you want to always perform a partial match:

    [[ $vvar == *$~filter* ]]

Other cases in your if-else chain also look suspiciously
non-orthogonal. The orthogonal bits of matching are:

1. Pattern matching or regex?
2. Case sensitive or not?
3. Partial or full?

There are a total of 8 combinations. If you drop regex (which you
probably want to do), it leaves 4 combinations.

    [[ $data == $~pattern ]]  # case sensitive, full
    [[ $data == (#i)$~pattern ]]  # case insensitive, full
    [[ $data == *$~pattern* ]]  # case sensitive, partial
    [[ $data == (#i)*$~pattern* ]]  # case insensitive, partial

Note that you don't need to quote $data here (although you can, if you
prefer to do it for stylistic reasons).

> ... the function let's me search for directories with automatic
> wildcards and/or case sensitivity  or both or neither.

There might be a better way to do this which would take advantage of
**/*. It's hard to say without knowing what you are trying to achieve.

Roman.




Messages sorted by: Reverse Date, Date, Thread, Author