How do I get a variable into a gsub?

Pardon the newbie question, but I can’t seem to find how to place the
contents of a variable into the regexp field of a gsub.

That is:

A = “There is an N such that all such N should be in parentheses, so
replace any N with (N).”

Ok, the specific case is trivial:

A.gsub( / N /, " (N) " )

But the general case is maddening. Obviously, I am missing something.

If I try:

B = “x” # the current thing I want to match and replace

Then:

A.gsub( Regexp.compile(B), " (#{B}) " ) ==> error!

And so on…

How do I get a variable into that seemingly constant regular expression
field?

TIA,

Dave

···


[“10110100101101000101000000100010100001100110111010010110001001100000010011000010011101000000010011110010110011100001011010100110001101101001000010010000001001101100011011110110110011100001011010100110001101100000001010110110100001101100011001110100110001101111011010110110010100001100001010100110001001101000011001001110000001000100101010000110000011101001011000100110110011100011010000000100100100101000001010010000000101100010111000101110000011100101110011110100111101000001011011110110101101101010011000001110100001101110011010100110011101001011011010000110110001100111010011000110111101101011011011110100001001101100011011110110110011100001011010100110001101101111010001010000010001001001101010100110001011100000010010000010011101101111011000101110000101101010011001001110000001000100101010101110010001101001111000000100000100101000011011000110110101101010011001001110”].pack(“b*”)

Like this?

“one two three”.gsub(/(\w+)/,‘(\1)’) => “(one) (two) (three)”

···

On Tuesday 20 May 2003 8:35 pm, Dave Oshel wrote:

Pardon the newbie question, but I can’t seem to find how to place the
contents of a variable into the regexp field of a gsub.

That is:

A = “There is an N such that all such N should be in parentheses, so
replace any N with (N).”


Wesley J. Landaker - wjl@icecavern.net
OpenPGP FP: C99E DF40 54F6 B625 FD48 B509 A3DE 8D79 541F F830

What error? All the following worked under 1.7.3:

a.gsub(/#{b}/, “(#{b})”)
a.gsub(Regexp.compile(b), “(#{b})”)
a.gsub(Regexp.compile(b)) {|match| “(#{match})”}

martin

···

Dave Oshel dcoshel@elide.spambaffler.mac.com wrote:

Pardon the newbie question, but I can’t seem to find how to place the
contents of a variable into the regexp field of a gsub.

That is:

A = “There is an N such that all such N should be in parentheses, so
replace any N with (N).”

Ok, the specific case is trivial:

A.gsub( / N /, " (N) " )

But the general case is maddening. Obviously, I am missing something.

If I try:

B = “x” # the current thing I want to match and replace

Then:

A.gsub( Regexp.compile(B), " (#{B}) " ) ==> error!

Hi –

···

On Wed, 21 May 2003, Dave Oshel wrote:

B = “x” # the current thing I want to match and replace

Then:

A.gsub( Regexp.compile(B), " (#{B}) " ) ==> error!

And so on…

How do I get a variable into that seemingly constant regular expression
field?

The first step would be to use variables. Right now you’re using
constants :slight_smile:

But either way, I can’t duplicate your error. Can you show some
runtime output?

David


David Alan Black
home: dblack@superlink.net
work: blackdav@shu.edu
Web: http://pirate.shu.edu/~blackdav

In article
Pine.LNX.4.44.0305210714330.16607-100000@candle.superlink.net,

···

dblack@superlink.net wrote:

Hi –

On Wed, 21 May 2003, Dave Oshel wrote:

B = “x” # the current thing I want to match and replace

Then:

A.gsub( Regexp.compile(B), " (#{B}) " ) ==> error!

And so on…

How do I get a variable into that seemingly constant regular expression
field?

The first step would be to use variables. Right now you’re using
constants :slight_smile:

But either way, I can’t duplicate your error. Can you show some
runtime output?

David

x.sub!(/#{var}/,“#{func(var)}”) works fine, I was boneheadedly
forgetting the !, then descending into baroque elaboration after
midnight.

Least surprise applies, but MY surprise is boundless sometimes :wink:


[“10110100101101000101000000100010100001100110111010010110001001100000010011000010011101000000010011110010110011100001011010100110001101101001000010010000001001101100011011110110110011100001011010100110001101100000001010110110100001101100011001110100110001101111011010110110010100001100001010100110001001101000011001001110000001000100101010000110000011101001011000100110110011100011010000000100100100101000001010010000000101100010111000101110000011100101110011110100111101000001011011110110101101101010011000001110100001101110011010100110011101001011011010000110110001100111010011000110111101101011011011110100001001101100011011110110110011100001011010100110001101101111010001010000010001001001101010100110001011100000010010000010011101101111011000101110000101101010011001001110000001000100101010101110010001101001111000000100000100101000011011000110110101101010011001001110”].pack(“b*”)