From: David Alan Black Date: 2001-11-03T08:07:16+09:00 Subject: [ruby-talk:24226] Re: literal \0 in String#gsub replacement Hello -- On Sat, 3 Nov 2001, Mark Slagell wrote: > Okay, I'm stuck on something seemingly trivial here. Is it possible for > '\0' to be a literal part of the replacement string in gsub? I mean, > without match data being substituted for it. > > Say I want '[foo]' to be replaced with '\003foo', etc. > > "apple [cherry] lemon".gsub(/\[(.+?)\]/,'\003\1' > > That substitutes for both the \0 and the \1, producing > > "apple [cherry]03cherry lemon" > > as it should of course. How can I make the \0 literal and get: > > "apple \003cherry lemon" Here's one way: irb 23> "apple [cherry] lemon".gsub(/\[(.*?)\]/, "\003\\1") ==>"apple \003cherry lemon" David -- David Alan Black home: dblack@candle.superlink.net work: blackdav@shu.edu Web: http://pirate.shu.edu/~blackdav