Regex Question

M

Mike Flannigan

Got a pretty simple question for you'all. This matches the
number data shown below:

foreach (<DATA>) {
/^.+\s(\d+.\d+),\s-(\d+.\d+),.*$/;
print "$1 -- $2 \n";
push @array, $1, $2;
}

Now, just in case the single space is not before the numbers
like those shown below in DATA, I thought I'd put in a "?"
after the \s, and make it:
/^.+\s?(\d+.\d+),\s?-(\d+.\d+),.*$/;
it returns the $2 OK, but the $1 is only the last 3 digits of what
I expect. I expect
35.020041249 -- 94.3847918870
and get
249 -- 94.3847918870

What am I missing here?


__DATA__
TP,DMS, 35.020041249, -94.3847918870,12/31/1989,00:00:00,1
TP,DMS, 35.010973698, -94.3846837580,12/31/1989,00:00:00,0
TP,DMS, 35.002423715, -94.3837645520,12/31/1989,00:00:00,0
TP,DMS, 34.595735442, -94.3845292880,12/31/1989,00:00:00,0
TP,DMS, 34.594175007, -94.3845061190,12/31/1989,00:00:00,0
TP,DMS, 34.585702269, -94.3817021280,12/31/1989,00:00:00,0
TP,DMS, 34.575576402, -94.3814240620,12/31/1989,00:00:00,0
TP,DMS, 34.571204088, -94.3744192330,12/31/1989,00:00:00,0
TP,DMS, 34.561611241, -94.3742261350,12/31/1989,00:00:00,0
TP,DMS, 34.554861166, -94.3737394970,12/31/1989,00:00:00,0
TP,DMS, 34.552041565, -94.3726348980,12/31/1989,00:00:00,0
TP,DMS, 34.545121539, -94.3707114980,12/31/1989,00:00:00,0
TP,DMS, 34.544642592, -94.3654524020,12/31/1989,00:00:00,0
 
J

John J. Trammell

Got a pretty simple question for you'all. This matches the
number data shown below:

foreach (<DATA>) {
/^.+\s(\d+.\d+),\s-(\d+.\d+),.*$/;
print "$1 -- $2 \n";
push @array, $1, $2;
}

A couple of comments:

1. you're not checking that the regexp matched before using $1, $2
2. (\d+.\d+) doesn't match what I think you think it matches--maybe
you mean "(\d+\.\d+)"?
3. how about just split()ting this nice comma-delimited data?

my ($lat,$lon) = (split /,/, $_)[2,3]; # untested

4. The problem with adding the '?' to \s is that the space
was the only thing keeping the .+ from greedy-matching the
first part of your latitude.
 
U

Uri Guttman

PG> Where is this extra hypen being generated?

print "$1 -- $2 \n";

your brain as usual.


uri
 
M

Mike Flannigan

Tad said:
unpack() is useful with fixed width fields.


my( undef, $num1, undef, $num2 ) = unpack 'A8 A12 A3 A13', $_;

That is useful. Thanks for the pointer.
Pack and Unpack is something that is often overlooked by me
in favor of regex's. Not sure that will change, but these
functions appear very good for fixed width formats.


Mike
 
M

Mike Flannigan

Purl said:
#!perl

while (<DATA>)
{
($num_1, $num_2) = split (/,[ ]*-/, substr ($_, 8, 28));
$num_2 =~ tr/,//d;
print "$num_1 -- $num_2\n";
}

__DATA__
TP,DMS, 35.020041249,-94.3847918870,12/31/1989,00:00:00,1
TP,DMS, 35.010973698, -94.3846837580,12/31/1989,00:00:00,0
TP,DMS, 35.002423715,-94.3837645520,12/31/1989,00:00:00,0
TP,DMS, 34.595735442, -94.3845292880,12/31/1989,00:00:00,0

PRINTED RESULTS:
________________

35.020041249 -- 94.3847918870
35.010973698 -- 94.3846837580
35.002423715 -- 94.3837645520
34.595735442 -- 94.3845292880

Pretty cool. I didn't know you could put an optional [] in
a split command, along with a quantifier like *. How
interesting (and dangerous). Here is a key line I bypassed:

"The delimiters are determined by repeated pattern matching,
using regular expressions given in pattern, so the delimiters
may be of any size and need not be the same string on
every match."

Very interesting.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,129
Messages
2,570,770
Members
47,329
Latest member
FidelRauch

Latest Threads

Top