zsh-users
 help / color / mirror / code / Atom feed
* Expanding when matching
@ 2004-04-23 21:15 DervishD
  2004-04-23 22:05 ` Wayne Davison
  2004-04-24  4:14 ` Bart Schaefer
  0 siblings, 2 replies; 5+ messages in thread
From: DervishD @ 2004-04-23 21:15 UTC (permalink / raw)
  To: Zsh Users

    Hi all :)

    I have a couple of doubts about Parameter Expansion, namely in
the replacement field. The first doubt is just curiosity, of the kind
that kills cats... The end of string anchor doesn't seems to work:

    $ testvar="This is my test var"
    $ print ${testvar/This*/Replaced}
    Replaced
    $ print ${testvar/#This*/Replaced}
    Replaced
    $ print ${testvar/var/Replaced}
    This is my test Replaced
    $ print ${testvar/var%/Replaced}
    This is my test var
    $ print ${testvar/#This*%/Replaced}
    This is my test var

    The example doesn't make much sense, but it illustrates what I
mean. What I'm doing wrong? What am I not understanding?

    The second problem is more difficult, I'm afraid. This is best
explained with an example:

    counter=0 ; for number in {1..100}
    do
        print ${number/*0*/Hit a ten! $((counter++))}
    done

    What I want is the substitution (and the postincrement) expanded
and run only when the parameter matches, but instead it is
(correctly) expanded on each call. This is just an example, I'm not
trying to count 'tens', what I really need is a way of doing the
expansion of the replacement expression only when it is a match.

    How can I achieve this with zsh? BTW, what I'm trying to do is to
replace some numbers in one text that should be consecutive but are
not, and they are interspersed with arbitrary text. What I know about
them is that they are alone in a line, so they match certain regex,
no problem about that. But between two of those numbers may be two,
four, ten lines, I don't know :((

    I know, I can do it with awk or sed, but I want to do it with
zsh, just for the sake of it O:) Thanks a lot in advance and sorry
for the noise. Hope some day I'm able to return all the help I get
here :) Thanks, I mean it.

    Raúl Núñez de Arenas Coronado

-- 
Linux Registered User 88736
http://www.pleyades.net & http://raul.pleyades.net/


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

* Re: Expanding when matching
  2004-04-23 21:15 Expanding when matching DervishD
@ 2004-04-23 22:05 ` Wayne Davison
  2004-04-24  7:48   ` DervishD
  2004-04-24  4:14 ` Bart Schaefer
  1 sibling, 1 reply; 5+ messages in thread
From: Wayne Davison @ 2004-04-23 22:05 UTC (permalink / raw)
  To: Zsh Users

On Fri, Apr 23, 2004 at 11:15:40PM +0200, DervishD wrote:
>     $ testvar="This is my test var"
>     $ print ${testvar/var%/Replaced}
>     This is my test var

This should be:

    $ print ${testvar/%var/Replaced}
    This is my test Replaced

The leading '%' indicates that the match must occur at the end.

> What I want is the substitution (and the postincrement) expanded and
> run only when the parameter matches

If you don't need it done in a single line, you can always use "case":

counter=0
for number in {1..100}; do
    case $number in
    *0) print "Hit a ten! $((counter++))" ;;
    *)  print $number ;;
    esac
done

..wayne..


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

* Re: Expanding when matching
  2004-04-23 21:15 Expanding when matching DervishD
  2004-04-23 22:05 ` Wayne Davison
@ 2004-04-24  4:14 ` Bart Schaefer
  2004-04-24  7:57   ` DervishD
  1 sibling, 1 reply; 5+ messages in thread
From: Bart Schaefer @ 2004-04-24  4:14 UTC (permalink / raw)
  To: Zsh Users

On Apr 23, 11:15pm, DervishD wrote:
}
}     $ print ${testvar/#This*%/Replaced}
}     This is my test var
} 
}     The example doesn't make much sense, but it illustrates what I
} mean. What I'm doing wrong? What am I not understanding?

I know Wayne has already answered, but:

The # and % tokens in ${var/pat/rep} are not analogous to regex ^ and $.
Rather, they're analogous to ${var#pat} and ${var%pat}.  Does that help?

For the analogs of ^ and $, you need (#s) and (#e) [and extendedglob].

    $ print ${testvar/(#s)This*var(#e)/Replaced}

} replace some numbers in one text that should be consecutive but are
} not, and they are interspersed with arbitrary text. What I know about
} them is that they are alone in a line, so they match certain regex,

    setopt extendedglob
    c=0; while read line; do print ${${line#<->(#e)}:-$[++c]}; done

(Replacing <-> with your pattern.)


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

* Re: Expanding when matching
  2004-04-23 22:05 ` Wayne Davison
@ 2004-04-24  7:48   ` DervishD
  0 siblings, 0 replies; 5+ messages in thread
From: DervishD @ 2004-04-24  7:48 UTC (permalink / raw)
  To: Wayne Davison; +Cc: Zsh Users

    Hi Wayne :)

 * Wayne Davison <wayned@users.sourceforge.net> dixit:
> >     $ testvar="This is my test var"
> >     $ print ${testvar/var%/Replaced}
> >     This is my test var
> This should be:
>     $ print ${testvar/%var/Replaced}
>     This is my test Replaced
> The leading '%' indicates that the match must occur at the end.

    I may have read the zsh info section 'Parameter Expansion'
hundreds of times, maybe more, and *always* I've read it, I've come
to the conclusion that the '#' and the '%' were the equivalent to '^'
and '$' in POSIX regexes, which they are not. The problem here is
that I did a bad reading of the manual, sorry :(( Since the symbols
used (# and %) are the same as in other parameter substitutions, that
should have worked as mnemonic O:)

    Again, sorry for the noise, next time I promise to better read
the manual, if I can afford the brain...
 
> > What I want is the substitution (and the postincrement) expanded and
> > run only when the parameter matches
> If you don't need it done in a single line, you can always use "case":

    The problem here is when the regex is not a simple number, but a
more complicated one which needs backrefs and the like when
substituting. Could it be done with 'case'?.

    Thanks a lot for your help, Wayne.

    Raúl Núñez de Arenas Coronado

-- 
Linux Registered User 88736
http://www.pleyades.net & http://raul.pleyades.net/


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

* Re: Expanding when matching
  2004-04-24  4:14 ` Bart Schaefer
@ 2004-04-24  7:57   ` DervishD
  0 siblings, 0 replies; 5+ messages in thread
From: DervishD @ 2004-04-24  7:57 UTC (permalink / raw)
  To: Bart Schaefer; +Cc: Zsh Users

    Hi Bart :)

 * Bart Schaefer <schaefer@brasslantern.com> dixit:
> }     $ print ${testvar/#This*%/Replaced}
> }     This is my test var
> }     The example doesn't make much sense, but it illustrates what I
> } mean. What I'm doing wrong? What am I not understanding?
> I know Wayne has already answered, but:
> The # and % tokens in ${var/pat/rep} are not analogous to regex ^ and $.
> Rather, they're analogous to ${var#pat} and ${var%pat}.  Does that help?

    It helps a lot, thanks :) As I told Wayne, I was doing a bad
reading of the manual, which clearly states that both symbols MUST BE
AT THE BEGINNING of the pattern...
 
> For the analogs of ^ and $, you need (#s) and (#e) [and extendedglob].
>     $ print ${testvar/(#s)This*var(#e)/Replaced}

    And this confused me the most, because I didn't know why the heck
zsh needed (#s) and (#e) having '#' and '%', which didn't need
extendedglob to be set. Now ALL makes sense. Thanks for the help :)
 
> } replace some numbers in one text that should be consecutive but are
> } not, and they are interspersed with arbitrary text. What I know about
> } them is that they are alone in a line, so they match certain regex,
>     setopt extendedglob
>     c=0; while read line; do print ${${line#<->(#e)}:-$[++c]}; done
> (Replacing <-> with your pattern.)

    I tried something similar (and completely wrong):

    counter=0; for number in {1..100}
    do
        print ${number/(*0*)/Hit a ten! ${match[1]:-$((counter++))}}
    done 

    Which obviously doesn't work neither, because the expansion takes
place no matter if a match occured or not. Your solution works
perfectly :) And I can use backrefs with it :))

    Thanks a lot, Bart :)

    Raúl Núñez de Arenas Coronado

-- 
Linux Registered User 88736
http://www.pleyades.net & http://raul.pleyades.net/


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

end of thread, other threads:[~2004-04-24  8:09 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2004-04-23 21:15 Expanding when matching DervishD
2004-04-23 22:05 ` Wayne Davison
2004-04-24  7:48   ` DervishD
2004-04-24  4:14 ` Bart Schaefer
2004-04-24  7:57   ` DervishD

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