0

I'm trying to phrase a line using regex but I've no idea how regex works.

This is the pattern I'm using now,

^(\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}).*?"(.*?/p/.*?,\d+,(\d+).*?)" "(\d+)" "(\d+)".*$

A sample line I would pharse using the pattern.

124.99.152.202 - naveen [22/Nov/2013:10:41:17 +1300] "GET /p/V4ZkA5d074CTy_vbFa7nLw,1385070078,888888888888888/FOLDER-NUMBER/i-dont-need-this-folder/nope/12.txt HTTP/1.1" "200" "8" "-" "Mozilla/5.0" "-"

Now the problem is I need to get one more bit of info from above sample line.

"GET /p/V4ZkA5d074CTy_vbFa7nLw,1385070078,888888888888888/FOLDER-NUMBER/12.txt HTTP/1.1"

How do I get this FOLDER-NUMBER integer value?

I tried this but I do not know how to filter it out, Regex Fiddle

^(\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}).*?"(.*?/p/.*?,\d+,(\d+)/**FOLDER-NUMBER**/.*?)" "(\d+)" "(\d+)".*$
4
  • 1
    This looks like a web server log. Microsoft makes a GREAT product called LogParser that lets you run all kinds of parsing schemes over log files. Commented Dec 8, 2013 at 1:54
  • Yes this is a linux server log. Thanks but I will be parsing using PHP. :) Commented Dec 8, 2013 at 1:55
  • is the folder number you want always the same number of folders deep? /p/XXXX/what_you_want? or is it sometimes different (in which case, you're going to find this a lot more challenging)? Commented Dec 8, 2013 at 2:02
  • Yes, it will be always there but the integer value is not the same /p/XXX,XXX,XXX/I-WILL-BE-ALWAYS-HERE/these-stuff-vary Commented Dec 8, 2013 at 2:05

1 Answer 1

1

Using your example, assuming you're always looking for the same number of folders deep, you could use:

\].*?\/.*?\/.*?\/(.*?)\/

The first matched group out of this will be the folder number you want, in the example provided.

See a working example: http://regex101.com/r/bO0aI1

To retrieve this via php, use:

preg_match(/\].*?\/.*?\/.*?\/(.*?)\//g, $yourLogLine, $matches);
echo $matches[1]; //the first matched group
Sign up to request clarification or add additional context in comments.

1 Comment

Great, thanks, I synced your code with the one I have, works perfectly. Thanks. :)

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.