zsh-users
 help / color / mirror / code / Atom feed
* Regular expressions in zsh
@ 2011-02-19  3:38 Vincent Stemen
  2011-02-19  5:48 ` Phil Pennock
  0 siblings, 1 reply; 5+ messages in thread
From: Vincent Stemen @ 2011-02-19  3:38 UTC (permalink / raw)
  To: zsh-users

I was very delighted to discover today that zsh now supports full
regular expressions in conditional statements, even using the Perl 
like =~ syntax.  That was one thing that I always wished zsh had.

My compliments to the chef :-).

Vince


^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: Regular expressions in zsh
  2011-02-19  3:38 Regular expressions in zsh Vincent Stemen
@ 2011-02-19  5:48 ` Phil Pennock
  2011-02-23 16:41   ` zzapper
  0 siblings, 1 reply; 5+ messages in thread
From: Phil Pennock @ 2011-02-19  5:48 UTC (permalink / raw)
  To: Vincent Stemen; +Cc: zsh-users

On 2011-02-18 at 21:38 -0600, Vincent Stemen wrote:
> I was very delighted to discover today that zsh now supports full
> regular expressions in conditional statements, even using the Perl 
> like =~ syntax.  That was one thing that I always wished zsh had.
> 
> My compliments to the chef :-).

zsh has had -pcre-match for a very long time; it's only the =~ operator
which is newish; note that if you're a Perl fan, then you might:

    setopt REMATCH_PCRE

to use PCRE regexps instead of the system regexp library's extended
regexp syntax.

You're welcome. :)  [though I can't claim credit for the original
-pcre-match].


^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: Regular expressions in zsh
  2011-02-19  5:48 ` Phil Pennock
@ 2011-02-23 16:41   ` zzapper
  2011-02-24 10:28     ` zzapper
  2011-02-24 11:21     ` Phil Pennock
  0 siblings, 2 replies; 5+ messages in thread
From: zzapper @ 2011-02-23 16:41 UTC (permalink / raw)
  To: zsh-users

Phil Pennock wrote in news:20110219054807.GA57597@redoubt.spodhuis.org:


> 
> zsh has had -pcre-match for a very long time; it's only the =~ operator
> which is newish; note that if you're a Perl fan, then you might:
> 
>     setopt REMATCH_PCRE
> 
> to use PCRE regexps instead of the system regexp library's extended
> regexp syntax.
> 
> You're welcome. :)  [though I can't claim credit for the original
> -pcre-match].
> 
Hi
Have you any illustrative examples of using REMATCH_PCRE?


-- 
zzapper
http://zzapper.co.uk/ Technical Tips


^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: Regular expressions in zsh
  2011-02-23 16:41   ` zzapper
@ 2011-02-24 10:28     ` zzapper
  2011-02-24 11:21     ` Phil Pennock
  1 sibling, 0 replies; 5+ messages in thread
From: zzapper @ 2011-02-24 10:28 UTC (permalink / raw)
  To: zsh-users

zzapper wrote in news:Xns9E95A9D1688C0zzappergmailcom@80.91.229.10:

> Phil Pennock wrote in news:20110219054807.GA57597@redoubt.spodhuis.org:
> 
> 
>> 
>> zsh has had -pcre-match for a very long time; it's only the =~ operator
>> which is newish; note that if you're a Perl fan, then you might:
>> 

with a little help from zsh-lovers webpage

zmodload zsh/pcre
setopt REMATCH_PCRE

var=ddddd; [[ "$var" =~ ^d+$ ]] && echo matched || echo did not match

var=dddee; regexp="^e+$"; [[ "$var" =~ $regexp ]] && echo $regexp matched 
$var || echo $regexp did not match $var

# above should be on one line


-- 
zzapper
http://zzapper.co.uk/ Technical Tips


^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: Regular expressions in zsh
  2011-02-23 16:41   ` zzapper
  2011-02-24 10:28     ` zzapper
@ 2011-02-24 11:21     ` Phil Pennock
  1 sibling, 0 replies; 5+ messages in thread
From: Phil Pennock @ 2011-02-24 11:21 UTC (permalink / raw)
  To: zsh-users

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.


^ permalink raw reply	[flat|nested] 5+ messages in thread

end of thread, other threads:[~2011-02-24 11:21 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2011-02-19  3:38 Regular expressions in zsh Vincent Stemen
2011-02-19  5:48 ` Phil Pennock
2011-02-23 16:41   ` zzapper
2011-02-24 10:28     ` zzapper
2011-02-24 11:21     ` Phil Pennock

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).