简体   繁体   中英

Regex expression to split string into items

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?

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.

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM