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