zsh-users
 help / color / mirror / code / Atom feed
From: Bart Schaefer <schaefer@brasslantern.com>
To: zsh-users@sunsite.dk
Subject: Re: Extracting Informations from strings
Date: Mon, 21 Jun 2004 09:28:46 -0700 (PDT)	[thread overview]
Message-ID: <Pine.LNX.4.44.0406210913370.32047-100000@toltec.zanshin.com> (raw)
In-Reply-To: <200406210933.i5L9XaKc003031@news01.csr.com>

On Mon, 21 Jun 2004, Peter Stephenson wrote:

> Stefan =?iso-8859-1?Q?Reich=F6r?= wrote:
> >   Battery 1: charging, 95%, charging at zero rate - will never fully charge.
> > 
> > I want to extract the "95%" from the output above.
> 
> setopt extendedglob
> local match mbegin mend
> if [[ $string = (#b)"Battery "*" charging, "(<->)"%"* ]]; then
>   print "Charge is ${match[1]}%"
> fi

To do this without extendedglob and backreferences is, I think, simpler:

	print Charge is ${(M)${(M)string%%<->%,*}##<->%}

This is (where <-> matches a string of digits):

	${(M)string%%<->%,*}	Longest suffix beginning with <->%
	${(M)...##<->%}		Longest prefix of that matching <->%

I'd only resort to backreferences when the substring to be extracted is
ambiguous (e.g. "... charging, 95%, charging at 17%, ...") or when
extracting multiple fields (e.g. both the battery number and the charge
percentage).


  reply	other threads:[~2004-06-21 16:29 UTC|newest]

Thread overview: 6+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2004-06-21  7:02 Stefan Reichör
2004-06-21  7:24 ` [zsh] " Artur Penttinen
2004-06-21 18:35   ` Vincent Stemen
2004-06-21  9:33 ` Peter Stephenson
2004-06-21 16:28   ` Bart Schaefer [this message]
2004-06-22  9:02 Elisabetta Riboldi

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=Pine.LNX.4.44.0406210913370.32047-100000@toltec.zanshin.com \
    --to=schaefer@brasslantern.com \
    --cc=zsh-users@sunsite.dk \
    /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).