literal \0 in String#gsub replacement 
Author Message
 literal \0 in String#gsub replacement

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"  

  Mark



Wed, 21 Apr 2004 06:57:28 GMT  
 literal \0 in String#gsub replacement
Hello --

Quote:

> 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


Web:  http://pirate.shu.edu/~blackdav



Wed, 21 Apr 2004 07:07:16 GMT  
 literal \0 in String#gsub replacement

Quote:
> Here's one way:

>   irb 23> "apple [cherry] lemon".gsub(/\[(.*?)\]/, "\003\\1")
>        ==>"apple \003cherry lemon"

Actually I tried that too in IRB but when I tried it in actual Ruby I
was surprised.  That results in the string containing the actual ASCII
character with code 003 (which printed as a heart on my Cygwin console.)
Since IRB actually calls "inspect" when it prints out, you got what you
saw.  To actually get a literal \003 in the resulting String, I found
that this worked:

"apple [cherry] lemon".gsub(/\[(.+?)\]/,'\\\003\1')

So Mark, if you want the ASCII character with code 003, use DAB's
method, but if you want the literal \003, then use the above.

Ryan Leavengood



Wed, 21 Apr 2004 07:17:05 GMT  
 literal \0 in String#gsub replacement
Hello --

Quote:

> > Here's one way:

> >   irb 23> "apple [cherry] lemon".gsub(/\[(.*?)\]/, "\003\\1")
> >        ==>"apple \003cherry lemon"

> Actually I tried that too in IRB but when I tried it in actual Ruby I
> was surprised.  That results in the string containing the actual ASCII
> character with code 003 (which printed as a heart on my Cygwin console.)
> Since IRB actually calls "inspect" when it prints out, you got what you
> saw.  To actually get a literal \003 in the resulting String, I found
> that this worked:

> "apple [cherry] lemon".gsub(/\[(.+?)\]/,'\\\003\1')

> So Mark, if you want the ASCII character with code 003, use DAB's
> method, but if you want the literal \003, then use the above.

I seem to be on a "heisenbug" spree: first getting bitten by shell
quoting, now this :-)

David

--
David Alan Black


Web:  http://pirate.shu.edu/~blackdav



Wed, 21 Apr 2004 07:19:17 GMT  
 
 [ 4 post ] 

 Relevant Pages 

1. gsub and literal strings

2. gsub() replacement containing replaced text

3. compare a string variable with a string literal

4. using gsub on a string containing single quotes

5. Problems with String#gsub!

6. My 1st look @ ruby: No prototypes and problem with String#gsub

7. My 1st look @ ruby: No prototypes and problem with String#gsub

8. String.gsub(), & RCR

9. String.gsub()

10. Variable width literal strings?

11. Differenty interpretation of literal bit string

12. Character literal visibility in string literals

 

 
Powered by phpBB® Forum Software