views:

22

answers:

1

I wrote a simple log file for my website that is in the following format:

TIME: "..." IP: "..." HOST: "..." UA: "..." 

And wanted to parse through it with sscanf as part of an exercise. My code was roughly like this, which was run on each line of the log:

list($time, $ip, $host, $ua) = sscanf("TIME: %s IP: %s HOST: %s UA: %s", $line);

Of course this is PHP, but it's irrelevant, now I thought this would work but it just doesn't parse anything. I know now that it's messing up becuase the user agent or time contains spaces, so it doesn't work.

I know not much about sscanf format, but wish to use it for simplicity, is there a way to read the quotes as a single entity (instead of using just plain %s) , so it can work correctly?

I got many logs in similar format, so this would be of great use if I can just get this example to work. Thank you!

A: 

I guess I'll just use a regular expression, such as "(?:[^\\"]+|\\.)*" to get it done.

John