Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Ruby > regex gsub

Reply
Thread Tools

regex gsub

 
 
Lorenzo Brito Morales
Guest
Posts: n/a
 
      02-26-2011
i have already working these phone.gsub(regex, "<
#{regex.match(phone)} >" ) but i think is another way to do it not?
like phone.gsub(regex,"< \1 >")
but the last instrucction does not bring the regex.match

what i want to do , is suppose you have the string "i love my dad "
the regex find dad then substitute dad <dad> , i have already solved
but i want
to know if the a fancy way to do the gsub like gsub(regex,"< \1 >")
instead of phone.gsub(regex, "< #{regex.match(phone)} >" )

 
Reply With Quote
 
 
 
 
Junegunn Choi
Guest
Posts: n/a
 
      02-26-2011
[Note: parts of this message were removed to make it a legal post.]

Try

"i love my dad".gsub(/dad/) { |match| "<#{match}>" }

or,

"i love my dad".gsub(/dad/) { "<#{$&}>" }

--
Cheers,
Junegunn Choi.

 
Reply With Quote
 
 
 
 
Harry Kakueki
Guest
Posts: n/a
 
      02-26-2011
On Sat, Feb 26, 2011 at 5:33 PM, Lorenzo Brito Morales
<(E-Mail Removed)> wrote:
> =A0i have already working these phone.gsub(regex, "<
> #{regex.match(phone)} >" ) but i think is another way to do it not?
> like phone.gsub(regex,"< \1 >")
> but the last instrucction does not bring the regex.match
>
> what i want to do , is suppose you have the string "i love my dad "
> the regex find dad then substitute dad <dad> , i have already solved
> but i want
> to know if the a fancy way to do the gsub like gsub(regex,"< \1 >")
> instead of phone.gsub(regex, "< #{regex.match(phone)} >" )
>
>


str =3D "i love my dad"
str2 =3D "I went to Baghdad"

p str.gsub(/(dad)/,'<\1>') #> "i love my <dad>"

p str2.gsub(/(dad)/,'<\1>') #> "I went to Bagh<dad>"


Do you also want that second match?





Harry

 
Reply With Quote
 
Brian Candler
Guest
Posts: n/a
 
      02-27-2011
Lorenzo Brito Morales wrote in post #984075:
> i have already working these phone.gsub(regex, "<
> #{regex.match(phone)} >" ) but i think is another way to do it not?
> like phone.gsub(regex,"< \1 >")
> but the last instrucction does not bring the regex.match


It's to do with how double-quoted strings interpret backslashes.

irb(main):001:0> "< \1 >"
=> "< \001 >"
irb(main):002:0> '< \1 >'
=> "< \\1 >"
irb(main):003:0> "< \\1 >"
=> "< \\1 >"
irb(main):004:0> "< \\1 >".size
=> 6
irb(main):005:0> puts "< \\1 >"
< \1 >
=> nil

Note that "< \\1 >" contains a *single* backslash - a backslash escaped
with a backslash

But you'd probably be better using the block form of gsub anyway:

phone.gsub(regex) { "< #{$&} >" }

--
Posted via http://www.ruby-forum.com/.

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
How make regex that means "contains regex#1 but NOT regex#2" ?? seberino@spawar.navy.mil Python 3 07-01-2008 03:06 PM
regex: \<start-of-word, and end-of-word\> not in gsub? Shea Martin Ruby 1 01-15-2007 08:15 PM
Crazy gsub/regex scheme - can this be done better? Wes Gamble Ruby 3 08-11-2006 11:49 PM
gsub and gsub! are inconsistent aurelianito Ruby 9 11-09-2005 01:38 PM
Regex back reference in gsub Dan Fitzpatrick Ruby 4 07-13-2005 10:31 PM



Advertisments