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.