Help with regex
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
ActivePerl [at] listserv.ActiveState.com
To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs
Re: Help with regex
Hello,
Maybe you need something like
$text =~ tr/ABCD/2223/;
?
Stanislaw Romanski
----- Original Message -----
From: "Barry Brevik" <BBrevik [at] StellarMicro.com>
To: <activeperl [at] listserv.ActiveState.com>
Sent: Thursday, August 13, 2009 6:52 PM
Subject: Help with regex
>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
> ActivePerl [at] listserv.ActiveState.com
> To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs
>
_______________________________________________
ActivePerl mailing list
ActivePerl [at] listserv.ActiveState.com
To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs
Re: Help with regex
This is a multipart message in MIME format.
--===============0354187723==
Content-Type: multipart/alternative;
boundary="=_alternative 0064DFF986257611_="
This is a multipart message in MIME format.
--=_alternative 0064DFF986257611_=
Content-Transfer-Encoding: 7bit
Content-Type: text/plain;
charset=us-ascii
The following script gives the following ouput
the take a look at the subroutine to better understand how the output is
formatted.
Phone Number Normalizer
Converting (763) Luv Perl to 763-588-7375
# ===============================SCRIPT
PhoneNormalize.pl================================
print "Phone Number Normalizer\n";
my $num = "(763) Luv Perl";
print "Converting $num to ". NormalizePhone($num) ."\n";
# ===========================================================
#This will normalize phone numbers to the format CCC-xxx-xxxx
# ===========================================================
sub NormalizePhone {
my $phone = shift;
# Removing spaces, and other delimiting characters
$phone =~ s/[-()\s+]//g;
# convert all alpha to uppercase
$phone = uc($phone);
# Convert Alpha to Numeric
$phone =~
tr/ABCDEFGHIJKLMNOPQRSTUVWXYZ/22233344455566677778889999/;
# Normalizing phone number
$phone =~ s/(\d{3})(\d{3})(\d{4})/$1-$2-$3/;
return $phone;
}
#
============================================================ ================================
Tony B. Okusanya
From:
"Stanislaw Romanski" <s.romanski [at] datos.pl>
To:
"Barry Brevik" <BBrevik [at] StellarMicro.com>,
<activeperl [at] listserv.ActiveState.com>
Date:
08/13/2009 12:20 PM
Subject:
Re: Help with regex
Sent by:
activeperl-bounces [at] listserv.ActiveState.com
Hello,
Maybe you need something like
$text =~ tr/ABCD/2223/;
?
Stanislaw Romanski
----- Original Message -----
From: "Barry Brevik" <BBrevik [at] StellarMicro.com>
To: <activeperl [at] listserv.ActiveState.com>
Sent: Thursday, August 13, 2009 6:52 PM
Subject: Help with regex
>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
> ActivePerl [at] listserv.ActiveState.com
> To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs
>
_______________________________________________
ActivePerl mailing list
ActivePerl [at] listserv.ActiveState.com
To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs
U.S. BANCORP made the following annotations
------------------------------------------------------------ ---------
Electronic Privacy Notice. This e-mail, and any attachments, contains information that is, or may be, covered by electronic communications privacy laws, and is also confidential and proprietary in nature. If you are not the intended recipient, please be advised that you are legally prohibited from retaining, using, copying, distributing, or otherwise disclosing this information in any manner. Instead, please reply to the sender that you have received this communication in error, and then immediately delete it. Thank you in advance for your cooperation.
------------------------------------------------------------ ---------
--=_alternative 0064DFF986257611_=
Content-Transfer-Encoding: 7bit
Content-Type: text/html;
charset=us-ascii
<br><font size=2 face="sans-serif">The following script gives the
following ouput</font>
<br><font size=2 face="sans-serif">the take a look at the subroutine to
better understand how the output is formatted.</font>
<br>
<br><font size=2 face="sans-serif">Phone Number Normalizer</font>
<br><font size=2 face="sans-serif">Converting (763) Luv Perl to 763-588-7375</font>
<br>
<br>
<br>
<br><font size=2 face="sans-serif"># ===============================SCRIPT
PhoneNormalize.pl================================</font>
<br><font size=2 face="sans-serif">print "Phone Number Normalizer\n";</font>
<br><font size=2 face="sans-serif">my $num = "(763) Luv Perl";</font>
<br><font size=2 face="sans-serif">print "Converting $num to ".
NormalizePhone($num) ."\n";</font>
<br>
<br><font size=2 face="sans-serif"># ===========================================================</font>
<br><font size=2 face="sans-serif">#This will normalize phone numbers to
the format CCC-xxx-xxxx</font>
<br><font size=2 face="sans-serif"># ===========================================================</font>
<br><font size=2 face="sans-serif">sub NormalizePhone {</font>
<br><font size=2 face="sans-serif"> my
$phone = shift;</font>
<br>
<br><font size=2 face="sans-serif"> #
Removing spaces, and other delimiting characters</font>
<br><font size=2 face="sans-serif"> $phone
=~ s/[-()\s+]//g;</font>
<br><font size=2 face="sans-serif"> </font>
<br><font size=2 face="sans-serif"> #
convert all alpha to uppercase</font>
<br><font size=2 face="sans-serif"> $phone
= uc($phone); </font>
<br>
<br><font size=2 face="sans-serif"> #
Convert Alpha to Numeric</font>
<br><font size=2 face="sans-serif"> $phone
=~ tr/ABCDEFGHIJKLMNOPQRSTUVWXYZ/22233344455566677778889999/;</font>
<br>
<br><font size=2 face="sans-serif"> #
Normalizing phone number</font>
<br><font size=2 face="sans-serif"> $phone
=~ s/(\d{3})(\d{3})(\d{4})/$1-$2-$3/;</font>
<br><font size=2 face="sans-serif"> return
$phone;</font>
<br><font size=2 face="sans-serif">}</font>
<br>
<br><font size=2 face="sans-serif"># ============================================================ ================================<br>
<br>
<br>
<br>
Tony B. Okusanya<br>
</font>
<br>
<br>
<br>
<table width=100%>
<tr valign=top>
<td><font size=1 color=#5f5f5f face="sans-serif">From:</font>
<td><font size=1 face="sans-serif">"Stanislaw Romanski" <s.romanski [at] datos.pl></font>
<tr valign=top>
<td><font size=1 color=#5f5f5f face="sans-serif">To:</font>
<td><font size=1 face="sans-serif">"Barry Brevik" <BBrevik [at] StellarMicro.com>,
<activeperl [at] listserv.ActiveState.com></font>
<tr valign=top>
<td><font size=1 color=#5f5f5f face="sans-serif">Date:</font>
<td><font size=1 face="sans-serif">08/13/2009 12:20 PM</font>
<tr valign=top>
<td><font size=1 color=#5f5f5f face="sans-serif">Subject:</font>
<td><font size=1 face="sans-serif">Re: Help with regex</font>
<tr valign=top>
<td><font size=1 color=#5f5f5f face="sans-serif">Sent by:</font>
<td><font size=1 face="sans-serif">activeperl-bounces [at] listserv.ActiveState.com</font></table>
<br>
<hr noshade>
<br>
<br>
<br><tt><font size=2>Hello,<br>
<br>
Maybe you need something like<br>
<br>
$text =~ tr/ABCD/2223/;<br>
<br>
?<br>
<br>
Stanislaw Romanski<br>
<br>
<br>
----- Original Message ----- <br>
From: "Barry Brevik" <BBrevik [at] StellarMicro.com><br>
To: <activeperl [at] listserv.ActiveState.com><br>
Sent: Thursday, August 13, 2009 6:52 PM<br>
Subject: Help with regex<br>
<br>
<br>
>I am using Active Perl 5.8.8. and Perl Dev Kit 7.0.<br>
> <br>
> I'm writing some code that standardizes the format of phone numbers
in a<br>
> database because users have entered them in every way imaginable.<br>
> <br>
> Anyway, some of the phone numbers look like this:<br>
> <br>
> 800-69-VORTEX<br>
> <br>
> ...where the user has entered alpha characters instead of numbers.
My<br>
> first thought was to use a hash to do the translation, like this:<br>
> <br>
> %alpha2num =<br>
> (<br>
> 'A' => '2',<br>
> 'B' => '2',<br>
> 'C' => '2',<br>
> 'D' => '3',<br>
> .<br>
> .<br>
> .<br>
> <br>
> ...but since the rest of the routine uses only regular expressions,
I<br>
> thought that it would be nice if this type of translation could be<br>
> accomplished with a regex.<br>
> <br>
> I have not been able to think of a way, but I thought I would put
it to<br>
> the list to see if anyone else has done this before.<br>
> <br>
> Barry Brevik<br>
> _______________________________________________<br>
> ActivePerl mailing list<br>
> ActivePerl [at] listserv.ActiveState.com<br>
> To unsubscribe: </font></tt><a href=http://listserv.activestate.com/mailman/mysubs><tt><font size=2>http://listserv.ActiveState.com/mailman/mysubs</font></tt></a><tt><font size=2><br>
><br>
_______________________________________________<br>
ActivePerl mailing list<br>
ActivePerl [at] listserv.ActiveState.com<br>
To unsubscribe: </font></tt><a href=http://listserv.activestate.com/mailman/mysubs><tt><font size=2>http://listserv.ActiveState.com/mailman/mysubs</font></tt></a><tt><font size=2><br>
</font></tt>
<br>
<br>
<P>U.S. BANCORP made the following annotations<br/> ------------------------------------------------------------ ---------<br/><font face="Arial" size=2>Electronic Privacy Notice. This e-mail, and any attachments, contains information that is, or may be, covered by electronic communications privacy laws, and is also confidential and proprietary in nature. If you are not the intended recipient, please be advised that you are legally prohibited from retaining, using, copying, distributing, or otherwise disclosing this information in any manner. Instead, please reply to the sender that you have received this communication in error, and then immediately delete it. Thank you in advance for your cooperation.</font><br>
<br>
------------------------------------------------------------ ---------<br/>
</P>
--=_alternative 0064DFF986257611_=--
--===============0354187723==
Content-Type: text/plain; charset="us-ascii"
MIME-Version: 1.0
Content-Transfer-Encoding: 7bit
Content-Disposition: inline
_______________________________________________
ActivePerl mailing list
ActivePerl [at] listserv.ActiveState.com
To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs
--===============0354187723==--
Re: Help with regex
This is a multipart message in MIME format.
--============== 56462222==
Content-Type: multipart/alternative;
boundary="=_alternative 0066558A86257611_="
This is a multipart message in MIME format.
--=_alternative 0066558A86257611_=
Content-Type: text/plain; charset="US-ASCII"
Cheap and dirty, cheezy, kindergarteny, but it does what you want:
use strict;
use warnings;
my $phnum = '847-VICTORY';
print "before: \"$phnum\"\n";
$phnum =~ s/[ABC]/2/ig;
$phnum =~ s/[DEF]/3/ig;
$phnum =~ s/[GHI]/4/ig;
$phnum =~ s/[JKL]/5/ig;
$phnum =~ s/[MNO]/6/ig;
$phnum =~ s/[PQRS]/7/ig;
$phnum =~ s/[TUV]/8/ig;
$phnum =~ s/[WXYZ]/9/ig;
# add hyphen
$phnum =~ s/-(\d\d\d)(\d\d\d\d)/-$1-$2/;
print "after: \"$phnum\"\n";
before: "847-VICTORY"
after: "847-842-8679"
Deane Rothenmaier
Programmer/Analyst
Walgreens Corp.
224-542-5150
I think you should profit from the mistakes of others. You don't live long
enough to make them all yourself. -- Lowell Ferguson
--=_alternative 0066558A86257611_=
Content-Type: text/html; charset="US-ASCII"
<br><font size=2 face="sans-serif">Cheap and dirty, cheezy, kindergarteny,
but it does what you want:</font>
<br>
<br><font size=2 face="Courier New">use strict;</font>
<br><font size=2 face="Courier New">use warnings;</font>
<br>
<br><font size=2 face="Courier New">my $phnum = '847-VICTORY';</font>
<br>
<br><font size=2 face="Courier New">print "before: \"$phnum\"\n";</font>
<br>
<br><font size=2 face="Courier New">$phnum =~ s/[ABC]/2/ig;</font>
<br><font size=2 face="Courier New">$phnum =~ s/[DEF]/3/ig;</font>
<br><font size=2 face="Courier New">$phnum =~ s/[GHI]/4/ig;</font>
<br><font size=2 face="Courier New">$phnum =~ s/[JKL]/5/ig;</font>
<br><font size=2 face="Courier New">$phnum =~ s/[MNO]/6/ig;</font>
<br><font size=2 face="Courier New">$phnum =~ s/[PQRS]/7/ig;</font>
<br><font size=2 face="Courier New">$phnum =~ s/[TUV]/8/ig;</font>
<br><font size=2 face="Courier New">$phnum =~ s/[WXYZ]/9/ig;</font>
<br>
<br><font size=2 face="Courier New"># add hyphen</font>
<br><font size=2 face="Courier New">$phnum =~ s/-(\d\d\d)(\d\d\d\d)/-$1-$2/;</font>
<br>
<br><font size=2 face="Courier New">print "after: \"$phnum\"\n";</font>
<br>
<br>
<br><font size=2 face="Courier New">before: "847-VICTORY"</font>
<br><font size=2 face="Courier New">after: "847-842-8679"</font>
<br>
<br><font size=2 face="sans-serif">Deane Rothenmaier<br>
Programmer/Analyst<br>
Walgreens Corp.<br>
224-542-5150<br>
<br>
I think you should profit from the mistakes of others. You don't live long
enough to make them all yourself. -- Lowell Ferguson</font>
--=_alternative 0066558A86257611_=--
--============== 56462222==
Content-Type: text/plain; charset="us-ascii"
MIME-Version: 1.0
Content-Transfer-Encoding: 7bit
Content-Disposition: inline
_______________________________________________
ActivePerl mailing list
ActivePerl [at] listserv.ActiveState.com
To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs
--============== 56462222==--
Re: Help with regex
Deane.Rothenmaier [at] walgreens.com wrote:
>
> Cheap and dirty, cheezy, kindergarteny, but it does what you want:
>
> use strict;
> use warnings;
>
> my $phnum = '847-VICTORY';
>
> print "before: \"$phnum\"\n";
>
> $phnum =~ s/[ABC]/2/ig;
> $phnum =~ s/[DEF]/3/ig;
> $phnum =~ s/[GHI]/4/ig;
> $phnum =~ s/[JKL]/5/ig;
> $phnum =~ s/[MNO]/6/ig;
> $phnum =~ s/[PQRS]/7/ig;
> $phnum =~ s/[TUV]/8/ig;
> $phnum =~ s/[WXYZ]/9/ig;
>
> # add hyphen
> $phnum =~ s/-(\d\d\d)(\d\d\d\d)/-$1-$2/;
>
> print "after: \"$phnum\"\n";
>
>
> before: "847-VICTORY"
> after: "847-842-8679"
Brute force method. :) But it won't work if there are () or
no - or 2 -'s etc. tr is much more efficient than your multiple
subs.
Logically you would want to :
1) remove all legal punctuation (and a leading 1 if present),
2) convert all alphas to their numeric equivalent,
3) reject anything that isn't 10 digits (possibly also 7 if
local numbers are allowed),
4) format the result the way you like.
_______________________________________________
ActivePerl mailing list
ActivePerl [at] listserv.ActiveState.com
To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs
Help with automated script
--===============0010539243==
Content-Type: multipart/alternative; boundary="0-1049513781-1250276462=:93231"
--0-1049513781-1250276462=:93231
Content-Type: text/plain; charset=utf-8
Content-Transfer-Encoding: quoted-printable
Dear Perl experts,
I have the following requirement which can be done in Perl/VB script whiche=
ver is smarted to do. I am new to perl and am sure i will learn a lot by yo=
ur valuable hints .
requirements are
- the=C2=A0log file should be deleted if the files=C2=A0are older than 400 =
days
- the=C2=A0log files from the=C2=A0server should be kept in the backup fold=
er for 185 days.
Script should check the following:
1. Check if a file c:\\emailmoniroing\\logs\EmailLogyymmdd.log=C2=A0exists =
where =E2=80=98yymmdd=E2=80=99 denotes today=E2=80=99s date If it does not,=
create a new file.
2. For each file in the =E2=80=98c:\\emailmonitor\main=E2=80=99 folder, che=
ck file size.
3. If file size=3D0, then move the file from the 'c:\\Emailmonitoring\main'=
folder to the =E2=80=98 c:\\emailmonitoring\backup=E2=80=99 folder
4. If file size >0, then read/append contents of the file to c:\emailmonito=
r\logs\EmailLogyymmdd.log. Move the file read from the
'c:\\Emailmonitoring\main' folder to the =E2=80=98 c:\\emailmonitoring\back=
up=E2=80=99 folder
5. Delete all files older than 'x'' months from the c:\\emailmonitoring\bac=
kup folder
6. Delete all EmailLogyymmdd.log files older than =E2=80=98x=E2=80=99 month=
s from the folder =E2=80=98c:\\emailmonitoring\Logs=E2=80=99
=C2=A0
Finally,
Set up a windows scheduler task to run the new script every 20 minutes or i=
t can run continuously, sleep and wake up every 5 minutes
=C2=A0
Could you provide any sample script something to acheive the above. it will=
be very useful to meet our business needs.
=C2=A0
Thanks in advance
regards
mani=0A=0A=0A See the Web's breaking stories, chosen by people lik=
e you. Check out Yahoo! Buzz. http://in.buzz.yahoo.com/
--0-1049513781-1250276462=:93231
Content-Type: text/html; charset=utf-8
Content-Transfer-Encoding: quoted-printable
<table cellspacing=3D"0" cellpadding=3D"0" border=3D"0" ><tr><td valign=3D"=
top" style=3D"font: inherit;"><DIV>Dear Perl experts,</DIV>
<DIV>I have the following requirement which can be done in Perl/VB script w=
hichever is smarted to do. I am new to perl and am sure i will learn a lot =
by your valuable hints .</DIV><FONT size=3D2>
<DIV>requirements are </DIV>
<DIV>- the log file should be deleted if the files are older than=
400 days</DIV>
<DIV>- the log files from the server should be kept in the backup=
folder for 185 days.</DIV>
<DIV>Script should check the following:</DIV></FONT>
<DIV>1. Check if a file <U>c:\\emailmoniroing\</U><A href=3D"file://logs/Em=
ailLogyymmdd.log"><FONT color=3D#0000ff>\logs\EmailLogyymmdd.log</FONT></A>=
exists where =E2=80=98yymmdd=E2=80=99 denotes today=E2=80=99s date If=
it does not, create a new file. </DIV>
<DIV>2. For each file in the =E2=80=98<U>c:\</U><A href=3D"file://emailmoni=
tor/main"><FONT color=3D#0000ff>\emailmonitor\main</FONT></A>=E2=80=99 fold=
er, check file size.</DIV>
<DIV>3. If file size=3D0, then move the file from the '<STRONG><U>c:</U></S=
TRONG><A href=3D"file://Emailmonitoring/main"><FONT color=3D#0000ff>\\Email=
monitoring\main</FONT></A>'<FONT color=3D#ff00ff> </FONT>folder to the =E2=
=80=98 <A href=3D"file://emailmonitoring/backup"><U><FONT color=3D#0000ff>c=
:\\emailmonitoring\backup</U></FONT></A>=E2=80=99 folder</DIV>
<DIV>4. If file size >0, then read/append contents of the file to <U>c:\=
e</U><A href=3D"file://Emailmonitoring/logs/EmailLogyymmdd.log"><FONT color=
=3D#0000ff>mailmonitor\logs\EmailLogyymmdd.log</FONT></A><U>.</U> Move the =
file read from the </DIV>
<DIV>'<U>c:</U><A href=3D"file://Emailmonitoring/main"><FONT color=3D#0000f=
f>\\Emailmonitoring\main</FONT></A>'<FONT color=3D#ff00ff> </FONT>folder to=
the =E2=80=98 <U>c:</U><A href=3D"file://emailmonitoring/backup"><FONT col=
or=3D#0000ff>\\emailmonitoring\backup</FONT></A>=E2=80=99 folder</DIV>
<DIV>5. Delete all files older than 'x'' months from the <U>c:</U><A href=
=3D"file://emailmonitoring/backup"><FONT color=3D#0000ff>\\emailmonitoring\=
backup</FONT></A> folder</DIV>
<DIV>6. Delete all EmailLogyymmdd.log files older than =E2=80=98x=E2=80=99 =
months from the folder =E2=80=98<U>c:</U><A href=3D"file://emailmonitoring/=
Logs"><FONT color=3D#0000ff>\\emailmonitoring\Logs</FONT></A>=E2=80=99</DIV=
>
<DIV> </DIV>
<DIV>Finally,</DIV>
<DIV>Set up a windows scheduler task to run the new script every 20 minutes=
or it can run continuously, sleep and wake up every 5 minutes</DIV>
<DIV> </DIV>
<DIV>Could you provide any sample script something to acheive the above. it=
will be very useful to meet our business needs. </DIV>
<DIV> </DIV>
<DIV>Thanks in advance</DIV>
<DIV>regards</DIV>
<DIV>mani</DIV></td></tr></table><br>=0A=0A=0A=0A <!--3--><hr size=3D1=
></hr> Looking for local information? Find it on <a href=3D"http://in.rd.ya=
hoo.com/tagline_local_1/*http://in.local.yahoo.com/" target=3D"_blank"> Yah=
oo! Local</a>
--0-1049513781-1250276462=:93231--
--===============0010539243==
Content-Type: text/plain; charset="us-ascii"
MIME-Version: 1.0
Content-Transfer-Encoding: 7bit
Content-Disposition: inline
_______________________________________________
ActivePerl mailing list
ActivePerl [at] listserv.ActiveState.com
To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs
--===============0010539243==--