Gsub for string

Hi,

I'm looking for a function that makes it possible to do the folowing:

Replace all substrings --xxxx-- with -xxxx--1 in a string.

The xxxx represents an unknown value (can be anything)

Is there a way to set unknow characters in a gsub function?

Thx!

You can match arbitrary characters and use capturing groups

str.gsub(/--([^-]+)--/, '-\\1--1')

Kind regards

robert

···

2010/3/1 Reinhart Viane <rv@domos.be>:

I'm looking for a function that makes it possible to do the folowing:

Replace all substrings --xxxx-- with -xxxx--1 in a string.

The xxxx represents an unknown value (can be anything)

Is there a way to set unknow characters in a gsub function?

--
remember.guy do |as, often| as.you_can - without end
http://blog.rubybestpractices.com/

Robert Klemme wrote:

You can match arbitrary characters and use capturing groups

str.gsub(/--([^-]+)--/, '-\\1--1')

which you can also do in a block form, which is sometimes clearer and
lets you do things like modifying xxxx.

str.gsub(/--([^-]+)--/) { "-#{$1}--1" }

···

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

For fairness reasons you should also mention that this is also slower
and not needed in this particular case since the replacement is fixed.
:slight_smile:

This is also a good opportunity to link to my most recent blog entry
which dealt with string replacements:

http://blog.rubybestpractices.com/posts/rklemme/020-Code_Massage.html

Cheers

robert

···

2010/3/1 Brian Candler <b.candler@pobox.com>:

Robert Klemme wrote:

You can match arbitrary characters and use capturing groups

str.gsub(/--([^-]+)--/, '-\\1--1')

which you can also do in a block form, which is sometimes clearer and
lets you do things like modifying xxxx.

str.gsub(/--([^-]+)--/) { "-#{$1}--1" }

--
remember.guy do |as, often| as.you_can - without end
http://blog.rubybestpractices.com/