[Date Prev][Date Next][Thread Prev][Thread Next] [Search] [Date Index] [Thread Index]

[MacPerl] Regexp help



This was also posted to comp.lang.perl.misc:



First off, thanks in advance. I mean it.

I need some pointers:

I have a file that contains data I need to split into variables.

Name            Url             State   Owner
-----           ---             -----   -----
Brown           http://etc       WA     WWP
Little Falls    http://etc       CA     PacifiCorp
Mossyrock       http://etc       ID     U.S. Gov


My variables:

$name
$url
$state
$owner

I tried spliting on the /\s+/, but that doesn't work of course because
of the case in #2 where the Name is 2 words or number three where the
Owner is also 2 relative words.

So can anyone give me any tips on where to learn about this moving
text around where you want it? I'll take page references I have the
llama book, the perl manpages, Tom Christiansen's Regular Expression
Description, and the Perl 5 Reference sheet.

I would like to learn the magic of Perl.

Tom Phoenix was kind enough to respond with:

>The trick is to figure out how _you_ know that, for example, the URL on
>the second line is not "Falls". Maybe the lack of :// tipped you off? :-)
>This _might_ be what you want. Hope this helps!

>   ($name, $url, $state, $owner) =
>        m#^(.+\b)\s+(\S+://\S+)\s+(\w\w)\s+(.*)#;



This works. Question: How does it work? Would someone care explain to me
how this works parses out the variables?

I can tell that m#...# sets the parameters for finding a match.

(\S+://\S+) seems to look for the url...but I haven't quite figured this
all out. I would be happy to re-examine the manpages or the llama book if I
could get a page reference. Thanks.



Darryl Caldwell             Oikiasuchou School
___________________________________________________
The Practice Hall: http://www.nesc.k12.ar.us/ph_stuff/ph.html
Myth & Martial Culture