Regular expression help

Status
Not open for further replies.
Looking for some help with a regular expression.

Got paragraphs of text that contain header lines consisting of a date, in the format of dd-mmm-yyyy HH:mm followed by someones name with a colon concatenated to their name.

E.g.
16-Sep-2010 15:35 Joe Bloggs: some text, some more text 18-Nov-2010 11:38 Patti Smith: some text, more text.
etc. etc.

Looking at a way to identify the date, name: bit so that we can embolden it and/or format it to a new line etc. etc.

Was thinking of running the text through a custom function which would jump through each word at a time (using a space as a delimiter) and using logic to find the start and end bits but it's not ideal, could be slow I guess and prone to errors.

So, another possibility that came to mind was using regular expressions to pattern match the header bits... but my regular expression foo is sorely lacking.

Anyone want to give me the regulat expression pattern match that would find the date, name: bits in the above example?

I.e. it needs to match to the start of the date in the format given and end with the colon after the name(s).

Cheers!
 

Dracorat

Ars Tribunus Militum
2,798
Recommendation for starting with Regular Expressions:
Buy this:
http://visibone.com/products/bbk14-15_425.html

As for your question:
(d{2}-\w{3}-\d{4})\s(\d{1,2}:\d{2})\s(\w+(?:\s\w+)+):

The first capture will have the date, the second will have the time and the third will have the name (captures are $1 $2 and $3)

Some languages don't recognize (?: sequences. If yours balks use this:
(d{2}-\w{3}-\d{4})\s(\d{1,2}:\d{2})\s(\w+(\s\w+)+):

It's essentially the same but there will be extra captures (that you won't use).
 
Thanks both, much appreciated!

Edit: is there a missing \ before the first d{2} ?

I've included the space place holders in the sub-expressions such:
(\d{2}-\w{3}-\d{4}\s)(\d{1,2}:\d{2}\s)(\w+(?:\s\w+)+:)

This works so far :) (thanks, I can actually see how it works as well!)

My issue now is, can the last sub-expression take into account one name, two names or any number of names before the final colon?
i.e. after the time and its following space, any number of characters (including spaces) before a final colon?

Edit2: Does (\d{2}-\w{3}-\d{4}\s)(\d{1,2}:\d{2}\s)(\w+:|\w+(?:\s\w+)+:) answer my own question? Seems to be not sure if it's optimum...
 
Status
Not open for further replies.