[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

Regexp grouping question

Peter Michaux

4/1/2006 9:08:00 PM

Hi

I'm trying to remove unecessary white space around equals signs and
after semicolons

compressed = "foo = bar; red = herring;"
compressed = compressed.gsub(/\s*([=;])\s*/,"#$1")
puts compressed

produces
foobarredherring

but I would like it to produce
foo=bar;red=herring;

Seems like the #$1 is not working. I also tried #{$1} but that didn't
work either.

Any ideas?

Thanks,
Peter

4 Answers

Chris Alfeld

4/1/2006 9:14:00 PM

0

compressed = "foo = bar; red = herring;"
compressed = compressed.gsub(/\s*([=;])\s*/,'\1')

I believe #$1 is evalued *before* gsub is executed and hence fails.
gsub sets the variable and uses \1, \2, \3 in the replacement string
to correspond.



On 4/1/06, petermichaux@gmail.com <petermichaux@gmail.com> wrote:
> Hi
>
> I'm trying to remove unecessary white space around equals signs and
> after semicolons
>
> compressed = "foo = bar; red = herring;"
> compressed = compressed.gsub(/\s*([=;])\s*/,"#$1")
> puts compressed
>
> produces
> foobarredherring
>
> but I would like it to produce
> foo=bar;red=herring;
>
> Seems like the #$1 is not working. I also tried #{$1} but that didn't
> work either.
>
> Any ideas?
>
> Thanks,
> Peter
>
>
>


Peter Michaux

4/1/2006 9:28:00 PM

0


Chris Alfeld wrote:
> compressed = "foo = bar; red = herring;"
> compressed = compressed.gsub(/\s*([=;])\s*/,'\1')
>
> I believe #$1 is evalued *before* gsub is executed and hence fails.
> gsub sets the variable and uses \1, \2, \3 in the replacement string
> to correspond.

Thank you!

Peter

Logan Capaldo

4/1/2006 10:27:00 PM

0


On Apr 1, 2006, at 4:28 PM, petermichaux@gmail.com wrote:

>
> Chris Alfeld wrote:
>> compressed = "foo = bar; red = herring;"
>> compressed = compressed.gsub(/\s*([=;])\s*/,'\1')
>>
>> I believe #$1 is evalued *before* gsub is executed and hence fails.
>> gsub sets the variable and uses \1, \2, \3 in the replacement string
>> to correspond.
>
> Thank you!
>
> Peter
>
>

If you want to use $1, $2, $3, etc. with gsub, you can use the block
form:

compressed.gsub(regexp) { |matched_string| $1 }



David Drub

6/22/2007 9:33:00 PM

0

unknown wrote:
> Hi
>
> I'm trying to remove unecessary white space around equals signs and
> after semicolons
>
> compressed = "foo = bar; red = herring;"
> compressed = compressed.gsub(/\s*([=;])\s*/,"#$1")
> puts compressed
>
> produces
> foobarredherring
>
> but I would like it to produce
> foo=bar;red=herring;
>
> Seems like the #$1 is not working. I also tried #{$1} but that didn't
> work either.
>
> Any ideas?
>
> Thanks,
> Peter

This thread has been *very helpful*. Thank you!

What if the input syntax allows spaces in the field values?

Possible input:
foo = high bar jump ; red = pickled herring ;

And, let's remove leading and trailing white space.

Desired results:
foo=high bar jump;red=pickled herring;

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