1

I have got this regex pattern that works with my current apache log format:

preg_match("/^(\S+) (\S+) (\S+) \[([^:]+):(\d+:\d+:\d+) ([^\]]+)\] \"(\S+) (.*?) (\S+)\" (\S+) (\S+) (\".*?\") (\".*?\")$/", $line, $matches); // pattern to format the line

It works with this log:

127.0.0.1 - - [19/Jun/2012:11:38:37 +0200] "GET /some_page HTTP/1.1" 200 8243 "http://example.com/referrer" "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/536.5 (KHTML, like Gecko) Chrome/19.0.1084.56 Safari/536.5"

Now I have changed the apache log format to include the server name, so the new log would be:

127.0.0.1 - - [19/Jun/2012:11:38:37 +0200] **servername.com** "GET /some_page HTTP/1.1" 200 8243 "http://example.com/referrer" "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/536.5 (KHTML, like Gecko) Chrome/19.0.1084.56 Safari/536.5"

The only thing it does it's adding servername.com before the "GET /url...".

Now the regex doesn't work anymore and I don't know what do I have to modify to make it match the new log format.

1 Answer 1

1

Here is your updated regex:

preg_match("/^(\S+) (\S+) (\S+) \[([^:]+):(\d+:\d+:\d+) ([^\]]+)\] [\w.]+ \"(\S+) (.*?) (\S+)\" (\S+) (\S+) (\".*?\") (\".*?\")$/", $line, $matches);

I've added an extra [\w.]+ group that should match your server name.

Sign up to request clarification or add additional context in comments.

2 Comments

Hi, it worked! Thanks! I have just added "(" and ")" around it, like this ([\w.]+) to be captured in the matches.
oh sure thing, i've forgot about those :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.