Hi,

I'm trying to write a patternmatching regexp, with two optional
parenthesis, but I can't figure out how to have an 'optional' match.
I.e. I want a match, regardless if the last two fields are available or
not. But if thy are available, I want to use them... I'm confident there
is a simple solution - I just haven't found it yet...

In practice:
The logfiles are from several Apache webservers. Some files contain two
additonal fields containing Referer and Browser type, which are last on
each line (example below, may be wrapped).

10.0.0.1 - - [30/Aug/2001:14:58:16 +0200] "GET /banner_1.gif HTTP/1.1"
200 12796 
"http://example.com/"; "Mozilla/5.0 (Windows; U; Win98; en-US; m18)
Gecko/20010131 Netscape6/6.01" 

My Patternmatching code look s as follows:

    if ( m/^(\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})   # IP Address
         \x20(.+?)                        # User
         \x20(.+?)                        # unused
         \x20(\[.+\])                     # Date
         \x20\"(.*?\n*?.*?)               # Request
         (HTTP\/.*?|)\"                   # Match regardless of HTTP
Version.
         \x20(\d+?)                       # Statuscodes
         \x20([\-\d]+?)                   # Size
         \x20(\".*?\")                    # Optional Referer
         \x20(\".*?\")                    # Optinal Browser type
         /ox )

However, it's the last two fields ($9 and $10) that I want to be
optional. If they don't exist in the current line being matched, I still
want the rest of the fields to be populated ($1 - $8). I.e. an
'optional' match...

On alternativ is to have two different pattern matching statements, but
that would complicate matters. There are more 'optionals' than just
these examples...

Any help would be greatly apprecieted. And yes, I have read the docs,
but simply not understood them.

TIA,

~Mike


Mike Blomgren
CCNOX Security Management & Technology AB 
Box 5227 
102 45  STOCKHOLM

www.ccnox.com
_____________________________________________________________ 
The information included in this e-mail is intended only for the 
person or entity to which it is addressed. Any use of this 
information by persons or entities other than the intended 
recipient is prohibited. If you receive this transmission in 
error, please delete this email and destroy any copies of it. 

Any opinions expressed in this email are those of the individual 
and not necessarily those of the company CCNOX. 

_______________________________________________
ActivePerl mailing list
[EMAIL PROTECTED]
To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs

Reply via email to