[lnkForumImage]
TotalShareware - Download Free Software

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


 

Forums >

comp.lang.ruby

Replacing part of a matched regular expression using gsub

Ben

3/24/2008 9:36:00 PM

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
4 Answers

Jesús Gabriel y Galán

3/24/2008 10:39:00 PM

0

On Mon, Mar 24, 2008 at 10:40 PM, Ben <coppin@gmail.com> wrote:

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

Ben

3/24/2008 11:12:00 PM

0

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

Paul Mckibbin

3/25/2008 2:07:00 AM

0

Rodrigo Bermejo 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
--
Posted via http://www.ruby-....

Robert Klemme

3/25/2008 10:27:00 AM

0

2008/3/25, Ben <coppin@gmail.com>:
> 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)").

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

--
use.inject do |as, often| as.you_can - without end