Replacing part of a matched regular expression using gsub

B

Ben

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
 
J

Jesús Gabriel y Galán

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.

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.
 
B

Ben

Hi Jesus,
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.

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
 
P

Paul Mckibbin

Rodrigo said:
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
 
R

Robert Klemme

2008/3/25 said:
Hi 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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,537
Members
45,020
Latest member
GenesisGai

Latest Threads

Top