On Thu, Jan 3, 2013 at 3:37 AM, Hamann, T.D. <thomas.ham...@naturalis.nl>wrote:

> > and then using a second regular expression to remove the space, but
> > that somehow seems silly. Surely there is a quicker way to do this?
>
> s/(i)(\d\d)(o)/1${2}0/;
>

One route, esp. if there might be more letter<->number pairs (like "leet"
(e.g. as "1337") or a list of common numeric typos, maybe) is to use the
'/e' modifier and a lookup table:
my %leet_pairs = (
    i => 1,
    o => 0,
     e => 3,
);
s/(\w)(\d{2})(\w)/$leet_pairs{$1} . $2 . $leet_pairs{$3}/e;

so i99o and o99i would both be handled. 'course you'd need to test if the
word char was in there and then
s/(\w)(\d{2})(\w)/
  exist $leet_pairs{$1} ? $leet_pairs{$1} : $1
   . $2 .
  exist $leet_pairs{$3} ? $leet_pairs{$3} : $3
/xe;

(in theory - sorry, didn't test this code) and that way lies madness,
though it does give you a reason to capture everything ;->. But at this
point, I'd build a function returning the modified string as better able to
handle exceptions, log bad data etc.

-- 

a

Andy Bach,
afb...@gmail.com
608 658-1890 cell
608 261-5738 wk

Reply via email to