Unless you post a complete example of the line to be parsed,
we cannot help you. Since I am a nice guy, though, I will
explain why you need more than
split for this problem.
The better solution would be to use the m// operator and
the grouping variables: $1, $2, etc. I'll explain by
parsing an entry from an Apache web server access log
$line = '127.0.0.1 - - [26/Mar/2001:16:01:07 -0500] "GET /stuff/ HTTP/
+1.0" 200 11874'
Each entry is seperated by dashes, brackets, or quotes -
but since we know the general layout, we can write a regualar
expression that is general enough to parse each line, but
specific enough to get the data we want - just the IP of
the referrer, the date stamp and the requested document (
along with request type)
use strict;
my ($ip,$date,$method,$file,$header,$status,$pid) = $line =~
/^([\d.]+) # $id = ip quad
\s*-\s*-\s* # skip over these
\[(.*?)\]\s" # $date = everything between the brackets
(\w+)\s* # $method = the method, usually GET or POST
([^\s]+)\s* # $file = everything UP TO the next white space
(.*)"\s* # $header = everything UP TO the next double quote
(\d+)\s* # $status = digits between spaces
(\d+)\s*$/x; # $pid = last set of digits
print "$ip\n$date\n$method\n$file\n$status\n$pid\n";
By no means I am an master of regular expressions, the ones
I chose just happen to work - there are better ways then using
.* - but a little badness won't kill ya' :)
Big Thanks to Albannach.
Jeff
R-R-R--R-R-R--R-R-R--R-R-R--R-R-R--
L-L--L-L--L-L--L-L--L-L--L-L--L-L--
-
Are you posting in the right place? Check out Where do I post X? to know for sure.
-
Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
<code> <a> <b> <big>
<blockquote> <br /> <dd>
<dl> <dt> <em> <font>
<h1> <h2> <h3> <h4>
<h5> <h6> <hr /> <i>
<li> <nbsp> <ol> <p>
<small> <strike> <strong>
<sub> <sup> <table>
<td> <th> <tr> <tt>
<u> <ul>
-
Snippets of code should be wrapped in
<code> tags not
<pre> tags. In fact, <pre>
tags should generally be avoided. If they must
be used, extreme care should be
taken to ensure that their contents do not
have long lines (<70 chars), in order to prevent
horizontal scrolling (and possible janitor
intervention).
-
Want more info? How to link
or How to display code and escape characters
are good places to start.