[Shootout-list] rexexmatch capture solution
skaller
skaller@users.sourceforge.net
14 Nov 2004 20:57:21 +1100
Argg I'm an idiot. Here is a Perl solution to the capture problem.
Note this
(a) doesn't use the ugly non-regular back references
(b) actually gets the right answer
-----------------------
#!/usr/bin/perl
# $Id: regexmatch-perl.code,v 1.4 2004/11/13 07:42:37 bfulgham Exp $
# http://www.bagley.org/~doug/shootout/
use strict;
my $re = qr{
(?: ^ | [^\d]) #0
( #1
\((\d\d\d)\) #2
|
(\d\d\d) #3
)
[ ]
(\d\d\d) #4
[ -]
(\d\d\d\d) #5
\D
}x;
my $NUM = $ARGV[0];
$NUM = 1 if ($NUM < 1);
my @phones = <STDIN>;
my $count = 0;
my $num;
while ($NUM--) {
foreach (@phones) {
if (/$re/o) {
$num = "($2$3) $4-$5";
if (0 == $NUM) {
$count++;
print "$count: $num\n";
}
}
}
}
-----------------------
Output:
-----------------------
1: (111) 111-1111
2: (111) 222-2222
3: (111) 333-3333
4: (111) 444-4444
5: (111) 555-5555
6: (111) 666-6666
7: (111) 777-7777
8: (111) 888-8888
9: (111) 999-9999
10: (111) 000-0000
11: (111) 232-1111
12: (111) 242-1111
13: (213) 222-2222
-----------------------
This agrees with the Felix solution (except I put a space
instead of a - as the last separator, now fixed :)
--
John Skaller, mailto:skaller@users.sf.net
voice: 061-2-9660-0850,
snail: PO BOX 401 Glebe NSW 2037 Australia
Checkout the Felix programming language http://felix.sf.net