zsh-users
 help / color / mirror / code / Atom feed
From: Phil Pennock <zsh-workers+phil.pennock@spodhuis.org>
To: zsh-users@zsh.org
Subject: Re: [[ 'abcde' =~ (#i)Bcd ]]
Date: Tue, 8 Nov 2022 12:40:26 -0500	[thread overview]
Message-ID: <Y2qUitMtpVkcdH4U@fullerene.field.pennock-tech.net> (raw)
In-Reply-To: <9bc02014-76ca-b2c0-5f7c-284b90884225@eastlink.ca>

On 2022-11-07 at 13:10 -0800, Ray Andrews 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? It seems to break no rules to make
> 'Bcd' case insensitive and then find it within 'abcde'.  Is there a
> workaround?

 *  =   : equivalent to "==", string comparison with globs supported
 *  =~  : regular expression match, syntax from Perl, used in bash
 *  -regex-match : operator for very explicit regexp match
 *  -pcre-match : operator for very explicit regexp match

In zsh, = and == came first, then -pcre-match.
The =~ operator from Perl was added to bash and I added support to zsh,
and wrote the zsh/regex module so that _by default_ zsh would be
compatible with bash.

Using `setopt pcre_match` will switch =~ from bash-compatible to using
PCRE, Perl Compatible Regular Expressions, so much closer to the
original =~.

The downside of PCRE in zsh is that for licensing reasons, not all
distributions include it.  Zsh itself is BSD-licensed, PCRE is not.

If PCRE is available, then:

  [[ 'abcde' =~ (?i)Bcd ]] && echo match3

Use `man pcrepattern` and look at "INTERNAL OPTION SETTING" to see how
(?something) turns on options, with 'i' being PCRE_CASELESS.  This
syntax matches Perl.

If PCRE is not available, then you are stuck with ERE syntax
(see `man 7 regex`) and you'll have to be a lot more explicit.  So
probably better to find ways to use zsh glob pattern matching instead of
regular expressions.

-Phil


  parent reply	other threads:[~2022-11-08 17:41 UTC|newest]

Thread overview: 13+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-11-07 21:10 Ray Andrews
2022-11-07 21:26 ` Roman Perepelitsa
2022-11-07 21:47   ` Ray Andrews
2022-11-07 22:15     ` Lawrence Velázquez
2022-11-08  1:57       ` Ray Andrews
2022-11-07 21:50   ` Lawrence Velázquez
2022-11-08  2:05     ` Ray Andrews
2022-11-08  8:19       ` Roman Perepelitsa
2022-11-08 13:32         ` Ray Andrews
2022-11-08 14:37           ` Roman Perepelitsa
2022-11-08 14:30             ` Ray Andrews
2022-11-08 17:40 ` Phil Pennock [this message]
2022-11-08 18:43   ` Ray Andrews

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=Y2qUitMtpVkcdH4U@fullerene.field.pennock-tech.net \
    --to=zsh-workers+phil.pennock@spodhuis.org \
    --cc=zsh-users@zsh.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this public inbox

	https://git.vuxu.org/mirror/zsh/

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).