Replacing part of a matched regular expression using gsub

Hi,

I’m trying to run through a piece of text replacing every character
with a context sensitive number. Rather than go into the gory details,
hopefully the following will enough to make clear what I’m trying to
do.

Today is the first day I’ve written any ruby code, so please forgive
me if this is an incredibly dumb post!

Anyway, my aim is to find a character, let’s say ‘x’ in the context of
two other characters, let’s say ‘a’ and ‘b’. When I find ‘axb’ I want
to replace x with a number and a comma, so I should end up with
‘a102,b’.

I figured I could use \1 and \3 to put the a and b back in, so my code
looks like this:

    replacement = "\\1102\\3,"
    word.gsub!(rule.getRegExp,replacement)

rule.getRegExp returns a Regexp object which was created using the
following string:

“(a)(x)(b)”

Instead, it replaces “axb” with “102,”.

I’m guessing my problem is one (or more) of the following:

a) gsub doesn’t do this.
b) I’m using parentheses in my matching expression incorrectly.
c) I’m using \1 and \3 incorrectly.

Can anyone help?

Thanks!

Ben

On Mon, Mar 24, 2008 at 10:40 PM, Ben [email protected] wrote:

b) I’m using parentheses in my matching expression incorrectly.
c) I’m using \1 and \3 incorrectly.

Hi, it works for me:

irb(main):009:0> replacement = “\1102\3,”
=> “\1102\3,”
irb(main):010:0> word = “qweraxbtyuiop”
=> “qweraxbtyuiop”
irb(main):011:0> word.gsub!(Regexp.new("(a)(x)(b)"), replacement)
=> “qwera102b,tyuiop”

Hope this helps,

Jesus.

Hi Jesus,

Jesus.

Thank you! Actually, that did help, as it made me realise that what I
needed to do was try it out in irb. I got it to work now - thanks!
(For the record, the problem was that rule.getRegExp was returning
“(a)x(b)” instead of “(a)(x)(b)”).

Ben

you can do this:

word.gsub!(/axb/) {“102,”}

#Check this out:
word = “groupaxbas”
r=/(axb)/
r =~ word
p $1

world.gsub!(“war”,“love”)

Rodrigo B. wrote:

word.gsub!(/axb/) {“102,”}

Except that will consume the a and b in addition to the x.

Try:
word = “groupaxbas”
word.gsub!(/(a)x(?=b)/,’\1102,’)
p word

Unfortunately, Ruby’s regex processor doesn’t seem to have the zero
width positive lookbehind, so I couldn’t quite work out how not to
consume the preceding ‘a’, so I needed to group and replace it. Zero
width positive lookahead and negative lookahead both seem to work ok,
although I’ve yet to see any documentation on these.

Mac

2008/3/25, Ben [email protected]:

=> “qwera102b,tyuiop”

Hope this helps,

Jesus.

Thank you! Actually, that did help, as it made me realise that what I
needed to do was try it out in irb. I got it to work now - thanks!
(For the record, the problem was that rule.getRegExp was returning
“(a)x(b)” instead of “(a)(x)(b)”).

I’d prefer the first one because you do not need the second group as
far as I can see. So I’d do

str.gsub!( /(a)x(b)/, ‘\1102,\2’ )

Kind regards

robert

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs