I'd like to parse a log file that has escaped data contained within a string delimiter. I'm having a problem however as I can't figure out a regular expression to address scenarios where the string delimiter itself is escaped. Take for instance the following example:
'Cat', 17, 'Jack\'s house', 45
I'd like to parse the above line into 2 fields:
Cat
Jack\'s house
My solution initially was the following regex: '[^']*'
But this returned:
Cat
Jack\
Could someone help me w/this? Thanks in advance.
Is there a way to grab all 4 fields using a regex? I didn't think that would be possible b/c some of the fields aren't string delimited.
1 reply to this topic
#1
Posted 28 October 2011 - 10:07 AM
|
|
|
#2
Posted 28 October 2011 - 06:41 PM
re.findall("^\s*(.*?)\s*(\d{1,4})\s*(.*?)\s*(\d{1,4})$", "'Cat', 17, 'Jack\'s house', 45"):
This should work (for 1 to 4 digit numbers) and returns 4 fields, but it's probably slow though. As an alternative, you could just split the string by comma, strip white space and convert numbers to integers.
A conclusion is where you got tired of thinking.
#define class struct // All is public.
1 user(s) are reading this topic
0 members, 1 guests, 0 anonymous users


Sign In
Create Account

Back to top









