Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Fw: Help with regex

2 views
Skip to first unread message

Andy Bach

unread,
Aug 13, 2009, 12:58:17 PM8/13/09
to f...@perl.org
Seems like this could be fun/golf

----- Forwarded by Andy Bach/WIWB/07/USCOURTS on 08/13/2009 11:57 AM -----


I am using Active Perl 5.8.8. and Perl Dev Kit 7.0.

I'm writing some code that standardizes the format of phone numbers in a
database because users have entered them in every way imaginable.

Anyway, some of the phone numbers look like this:

800-69-VORTEX

...where the user has entered alpha characters instead of numbers. My
first thought was to use a hash to do the translation, like this:

%alpha2num =
(
'A' => '2',
'B' => '2',
'C' => '2',
'D' => '3',
.
.
.

...but since the rest of the routine uses only regular expressions, I
thought that it would be nice if this type of translation could be
accomplished with a regex.

I have not been able to think of a way, but I thought I would put it to
the list to see if anyone else has done this before.

Barry Brevik
_______________________________________________
ActivePerl mailing list
Activ...@listserv.ActiveState.com
To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs

Adam Turoff

unread,
Aug 13, 2009, 1:23:03 PM8/13/09
to Andy...@wiwb.uscourts.gov, f...@perl.org
On Thu, Aug 13, 2009 at 12:58 PM, <Andy...@wiwb.uscourts.gov> wrote:
> Anyway, some of the phone numbers look like this:
>
>  800-69-VORTEX
>
> ...where the user has entered alpha characters instead of numbers. My
> first thought was to use a hash to do the translation, like this:
>
>  %alpha2num =
>  (
>    'A' => '2',
>    'B' => '2',
>    'C' => '2',
>    'D' => '3',
>    .
>    .
>    .
>
> ...but since the rest of the routine uses only regular expressions, I
> thought that it would be nice if this type of translation could be
> accomplished with a regex.

$phone =~ s/([A-Za-z])/$alpha2num{"\U$1"}/g;

Also handles the case of 650-Mr-Plow

-- Adam

Michael Running Wolf

unread,
Aug 13, 2009, 2:24:57 PM8/13/09
to Andy...@wiwb.uscourts.gov, f...@perl.org
This is a *blended* solution, combining the best of hashes (direct
lookup) and substitutions. Of course, you'll need to take care of
upper- and lower-case keys in the hash.

1 while s/ ([a-z]) / $alpha2num{$1} /iex;

Aside: It's always been a pet peeve of mine that many contact
managers don't let me store alpha characters, so if this is a "real
world" application, I'd suggest storing it how the user entered it,
then having a separate standardization operation that could be called
as needed. If it were an address book, I'd suggest displaying *both*
the raw and standard forms, allowing the user to edit the raw form,
and disabling the normaized form if there are no alphabeticals in the
raw form.

For example, something like this.

Raw (user editable) (optional normalization)
800-69-VORTEX -> 800-698-6783
800-EAT-AT-THE-Y -> 800-328-
800-555-1212

I like this because I like *both*. I like to remember the alphanum,
and dial the numeric. I don't like interfaces that remove one of the
parts that I like.

And alternative solution, if screen real estate is at a premium, would
be to have a onMouseOver event change the displayed text. That allows
me to store alphanumerics, but translate to numerics at my will.

--
Please take note of the changes:
Old: Michael Running Wolf <MichaelRu...@att.net
New: Michael R. Wolf <Michae...@att.net


Lev

unread,
Aug 13, 2009, 3:04:42 PM8/13/09
to Andy...@wiwb.uscourts.gov, f...@perl.org
On Thu, Aug 13, 2009 at 16:58, <Andy...@wiwb.uscourts.gov> wrote:
> Seems like this could be fun/golf
>
> ----- Forwarded by Andy Bach/WIWB/07/USCOURTS on 08/13/2009 11:57 AM -----
>
>
> I am using Active Perl 5.8.8. and Perl Dev Kit 7.0.
>
> I'm writing some code that standardizes the format of phone numbers in a
> database because users have entered them in every way imaginable.
>
> Anyway, some of the phone numbers look like this:
>
> 800-69-VORTEX
>
> ...where the user has entered alpha characters instead of numbers. My
> first thought was to use a hash to do the translation, like this:
>
> %alpha2num =
> (
> 'A' => '2',
> 'B' => '2',
> 'C' => '2',
> 'D' => '3',
> .
> .
> .
>
> ...but since the rest of the routine uses only regular expressions, I
> thought that it would be nice if this type of translation could be
> accomplished with a regex.

perl -le'y!SVYZ!789!,s!([A-X])!int ord($1)%59/3!ge,print for join"","A".."Z"'

--
lev

Michael Running Wolf

unread,
Aug 14, 2009, 1:06:52 AM8/14/09
to John Douglas Porter, f...@perl.org

On Aug 13, 2009, at 11:51 AM, John Douglas Porter wrote:

> Maybe this is too obvious...
> and I know it's not regex...
>
> tr/A-Z/22233344455566677778889999/;


It's a beautiful and elegant piece of SWIM (Say What I Mean) code.

The following formatting makes the mapping a bit more visually
obvious, especially with a fixed width font, though golfers may object
to the whitespace and the range expansion.

tr {abc def ghi jkl mno pqrs tuv wxyz}
{222 333 444 555 666 7777 888 9999};

Michael Wolf

RUR Admin

unread,
Aug 13, 2009, 2:51:11 PM8/13/09
to f...@perl.org
You can also try the translation operator:

tr/A-Za-z/2223334445556667777888999922233344455566677778889999/ ;

Sergio

David Rigaudiere

unread,
Aug 13, 2009, 1:09:23 PM8/13/09
to Andy...@wiwb.uscourts.gov, f...@perl.org

Hi,
Perhaps I misunderstood the problem but it seems simple with tr///;

Based on this layout http://upload.wikimedia.org/wikipedia/commons/thumb/d/dd/Mobile_phone_keyboard.svg/559px-Mobile_phone_keyboard.svg

tr/abcdefghijklmnopqrstuvwxyz/22233344455566677778889999/; #
should do the job (case-sensitive)


Regards.
David "Sniper" Rigaudiere

0 new messages