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

Re: Expanding when matching



    Hi Bart :)

 * Bart Schaefer <schaefer@xxxxxxxxxxxxxxxx> 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/



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