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

Re: Regular expressions in zsh



On 2011-02-23 at 16:41 +0000, zzapper wrote:
> Have you any illustrative examples of using REMATCH_PCRE?

I keep a shell variable $ipv6_regex defined, with a PCRE regexp that
matches IPv6 addresses.  Lots of non-capturing sub-expressions.

You can grab emit_ipv6_regexp from:
  http://people.spodhuis.org/phil.pennock/software/

Then it's just:
  [[ $foo =~ ^$ipv6_regex\$ ]] && echo IPv6

The other times it matters is when I'm using zero-width assertions,
which are Rather Nice in those situation where you need them.

As a rather contrived example, this will print out the name and value of
all shell variables with "NAME" in their name, except for ZSH_NAME:

for param in ${(k)parameters}; do
  [[ $param =~ '(?<!ZSH_)NAME' ]] || continue
  echo "$param: ${(P)param}"
done

I don't actually recall what I've used rematch_pcre for, besides the
IPv6 regexp, recently: it's just one of those things that when you need
it, it's very helpful.



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