On 11/09/15 16:40, Ron Piggott wrote:
Hi Everyone.
I've found this REGEX pattern at
https://coderwall.com/p/snn1ag/regex-to-parse-your-default-nginx-access-logs
(?P<ipaddress>\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}) - -
\[(?P<dateandtime>\d{2}\/[a-z]{3}\/\d{4}:\d{2}:\d{2}:\d{2}
(\+|\-)\d{4})\] ((\"(GET|POST) )(?P<url>.+)(http\/1\.1"))
(?P<statuscode>\d{3}) (?P<bytessent>\d+) (["](?P<refferer>(\-)|(.+))["])
(["](?P<useragent>.+)["])
I am missing why it isn't parsing my access log file:
108.170.163.140 - - [11/Sep/2015:09:59:54 -0400] "GET
/prayers-for-today/20 HTTP/1.0" 200 14982 "-" "Mozilla/5.0 (Linux;
Android 5.0.1; GT-I9505 Build/LRX22C) AppleWebKit/537.36 (KHTML, like
Gecko) Chrome/44.0.2403.133 Mobile Safari/537.36" "-"
Would someone with a fresh set of eyes have a look for me please?
This is the syntax where I use it:
if (preg_match($pattern, $line, $matches)) {
Thank you.
Ron
Hello Ron,
I think you mixed 2 languages, PHP and Python.
I'm not into Python, but the part <snip>(?P<ipaddress></snip> isn't a
valid PHP regex. I have a gut feeling this is a Python declaration that
would store the match, if found, in a variable called 'ipaddress'
For example, the PHP equivalent of the above IP-address regex would be:
/\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}/
Or better,
/^[\s*]\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}/ that forces the regex to
start at the beginning of the line, and catch any preceding whitespace
characters.
To get the matches saved, use
/(\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})/
One tip: unless you're really experienced with regex'es, go 1 character
match at a time. I learned that the hard way :-)
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php