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

Re: Expanding when matching



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



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