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

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



On Mon, Nov 7, 2022 at 10:11 PM Ray Andrews <rayandrews@xxxxxxxxxxx> wrote:
>
>
> [[ 'abcde' =~ 'bcd' ]] && echo match1
> [[ 'abcde' = (#i)ABcde ]] && echo match2
> [[ 'abcde' =~ (#i)Bcd ]] && echo match3
> [[ 'bcd' =~ 'abcde' ]] && echo match4
>
> ... I get match 1 and match 2.  I  understand not getting match 4
> because '=~' is not bi-directional, the latter value must be a subset of
> the former.  But why don't I get match 3?

Does it surprise you that this also doesn't match?

    [[ 'a' =~ (#i)A ]]

(#i) only works with pattern matching. For regex the easiest
workaround is to convert left-hand-side to lowercase:

    foo=XaBcX
    [[ ${(L)foo} =~ abc ]] && echo match

Another option is to use zsh/pcre module. See
https://zsh.sourceforge.io/Doc/Release/Zsh-Modules.html#The-zsh_002fpcre-Module.

In this specific case it's better to use pattern matching of course:

    [[ $foo == (#i)*abc* ]] && echo match

I find it extremely rare in practice that I need a regex match in zsh.

Roman.




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