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

Re: ideas



On Mar 18, 12:03am, Quinn Dunkan wrote:
} Subject: ideas
}
} ${...#...} and ${...%...} chop things off the begginning or ends
} of variables, and I've wondered why there isn't something like
} ${i:s/foo/bar} to replace foo in $i with bar. With ${(S)...%...}
} one can chop things out of the center of a variable, but what if
} you want to replace it with soething else?

In addition to ${var}(:s/foo/bar/) [which BTW in 3.0.5 requires the
trailing / to work correctly, a bug] there's always

	${var%foo*}bar${var#*foo}

Or even

	r=(${(BE)=var%foo*})
	l=(${(BE)=var#*foo})
	${var[$l[1],$r[1]-1]}bar${$var[$l[2],$r[2]-1]}

In which case the removed portion is

	${var[$r[1],$l[2]-1]}

} Actually, it would be even better if you could put in a regexp.

How often is a glob pattern not sufficient?  With extendedglob, you
can write almost any regexp as a glob.  The big thing you lose is the
ability to substitute the matches for sub-patterns of the regexp into
the replacement.

-- 
Bart Schaefer                                 Brass Lantern Enterprises
http://www.well.com/user/barts              http://www.brasslantern.com



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