tags:

views:

340

answers:

2

Hi,

I've got text in a form of

[1/12/2008 2:32:11 p.m. - name]
line 1
[1/12/2008 2:36:00 p.m. - name] - PRIVATE
line 2 [some text] sd
[1/12/2008 2:36:00 p.m. - name]
line 3

which i want to split into items so i have access to time, name and text of each item,

e.g.:
(item 1)
  1: 1/12/2008 2:32:11 p.m.
  2: name
  3: line 1
(item 2)
  1: 1/12/2008 2:36:00 p.m.
  2: name
  3:  - PRIVATE
    line 2 [some text] sd
(item 3)
  1: 1/12/2008 2:36:00 p.m.
  2: name
  3: line 3

I was trying to come up with a single regex pattern to achieve this, but no luck:

\[([0-9\/ \:\.apm]+?) - ([a-z_\-0-9]+?)\](.*?\r\n.+?)(?:\[[0-9\/ \:\.apm]+? - [a-z_\-0-9]+?\])

This pattern above only returns the first item.

\[([0-9\/ \:\.apm]+?) - ([a-z_\-0-9]+?)\](.*?\r\n.+?)(?!\[[0-9\/ \:\.apm]+? - [a-z_\-0-9]+?\])

The pattern above returns all items, but only first character of the text (group 3)

Any suggestions?

+4  A: 

It looks like this gets you what you want.

\[([0-9\/ \:\.apm]+?) - ([a-z_\-0-9]+?)\](.*?\r\n.+.*?)

I tested it out on my RegEx tester and it appears to get the proper format.

Mitchel Sellers
Thanks, i tried your example and i hasn't worked initially. Then it turned out that "single line" regex option was causing it not to work. Removing "single line" option solved it.
Muxa
Thanks for the link to the online regex tester 1+
CheGueVerra
@CheGueVerra - No problem! I'm hoping to release a new version also in the next few days, as there is a "bug" with the way that it renders HTML/XML matches.
Mitchel Sellers
A: 

Not quite an answer, but there is a tool called expresso that may be able to help you.

http://www.ultrapico.com/expresso.htm

CountCet