Reputation: 1066
I need to parse a plaintext file that looks like a log:
11/04/2015 11:45:01: James: Cheers guys, enjoy the weekend!
11/04/2015 12:08:55: Sarah: Sounds good James
11/04/2015 12:09:24: Sarah: What are the details of the trip?
11/04/2015 12:19:06: Leah: Driving up on Friday.
Saturday we'll hit the beach.
Sunday paaaaarty!
11/04/2015 12:29:54: James: Nice.
I'm currently parsing by line break:
var messages = data.split('\n');
But this doesn't work where a message contains a line break (see Leah's message above).
What would be the proper way to parse each new entry? Some kind of regular expression date/time match? or Some Regular Expression which parses date as mentioned above ?
Grateful your help.
Upvotes: 3
Views: 1962
Reputation: 3784
I think what you can try here is -
If each line stats with a date format then take later part of it as on string till it ends with the another date format.
Dont split using
\n
instead use the date that is inmm/dd/yyyy hh:mm:ss:
format .Logic needs to applied for below type because your text is in this type as mentioned below--
Date Format starts >> content << Date Format Ends
Make your own Regular Expression using this guide . http://www.w3schools.com/jsref/jsref_obj_regexp.asp
Try this Regular Expression to split /[0-9]+\/[0-9]+\/[0-9]* [0-9]*\:[0-9]*\:[0-9]*\:/g
var re = /[0-9]+\/[0-9]+\/[0-9]* [0-9]*\:[0-9]*\:[0-9]*\:/g;
var str = '11/04/2015 11:45:01: James: Cheers guys, enjoy the weekend!\n\n11/04/2015 12:08:55: Sarah: Sounds good James\n\n11/04/2015 12:09:24: Sarah: What are the details of the trip?\n\n11/04/2015 12:19:06: Leah: Driving up on Friday.\nSaturday we\'ll hit the beach.\nSunday paaaaarty!\n\n11/04/2015 12:29:54: James: Nice.';
var m;
while ((m = re.exec(str)) !== null) {
if (m.index === re.lastIndex) {
re.lastIndex++;
}
// View your result using the m-variable.
// eg m[0] etc.
}
Upvotes: 2
Reputation: 16968
I think you can use a regex like this:
/^[\d\/ :]+:[^:]+:(.*)|(.*)$/gm
Then you can use its substitutions: $1
and $2
Upvotes: 1