Quick questions on using regular expressino with gsub

Experts-

Quick question: I’m trying to use the regular expression “\d” to replace the characters “SP” followed by any single digit, with “SP” followed by the value in a string variable. However, this technique doesn’t appear to make the desired replacements:

pkg_base_temp.gsub!(/SP\d/, "SP"+sp_num_step)

is the target string “/SP\d/” not written correctly?

Thanks.

-Kurt

Hi,

Experts-
Naw, prolly not. But I’ll respond anyway :wink:

Quick question: I’m trying to use the regular expression “\d” to
replace the characters “SP” followed by any single digit, with
"SP" followed by the value in a string variable. However, this
technique doesn’t appear to make the desired replacements:

pkg_base_temp.gsub!(/SP\d/, “SP”+sp_num_step)

is the target string “/SP\d/” not written correctly?

It appears to work here on ruby 1.6.6 (2001-12-26) [i586-mswin32] -

irb(main):001:0> pkg_base_temp = " SP1 “
” SP1 "
irb(main):002:0> sp_num_step = "2"
“2"
irb(main):003:0> pkg_base_temp.gsub!(/SP\d/, “SP”+sp_num_step)
” SP2 “
irb(main):004:0> pkg_base_temp
” SP2 "

Regards,

Bill

···

From: “Kurt Euler” keuler@portal.com

In article C47CCC6238EFD4119C5200508B95A100074AFB51@cup1ex1.portal.com,
Kurt Euler wrote:

Experts-

Quick question: I’m trying to use the regular expression “\d” to
replace the characters “SP” followed by any single digit, with "SP"
followed by the value in a string variable. However, this technique
doesn’t appear to make the desired replacements:

pkg_base_temp.gsub!(/SP\d/, “SP”+sp_num_step)

is the target string “/SP\d/” not written correctly?

What error message are you getting? It may be that you’re expecting
perlish number <=> string magic to happen e.g.

pkg_base_temp = "fooSP2bar"
sp_num_step = 5
pkg_base_temp.gsub!(/SP\d/, “SP”+sp_num_step) => boom!

as 5 is not a string (TypeError: cannot convert Fixnum into String). If
this is the problem then

pkg_base_temp.gsub!(/SP\d/, “SP” + sp_num_step.to_s)

or

pkg_base_temp.gsub!(/SP\d/, “SP#{sp_num_step}”)

might be useful.

Hope this helps,

Mike

···


mike@stok.co.uk | The “`Stok’ disclaimers” apply.
http://www.stok.co.uk/~mike/ | GPG PGP Key 1024D/059913DA
mike@exegenix.com | Fingerprint 0570 71CD 6790 7C28 3D60
http://www.exegenix.com/ | 75D2 9EC4 C1C0 0599 13DA

In an attempt to throw the authorities off his trail, Kurt Euler keuler@portal.com transmitted:

Is it an “expressino” when you are writing your code at Starbucks?

Inquiring minds want to know…

···


(reverse (concatenate 'string “moc.enworbbc@” “enworbbc”))
http://www3.sympatico.ca/cbbrowne/commerce.html
"A LISP programmer knows the value of everything, but the cost of
nothing." – Alan Perlis