0

Hello I need help with Regular Expression,

I want to match each section (number and it's text - 2 groups), the text can be multi line, each section ends when another section starts (another number) or when .END is reached or EOF.
Demo

Expression:

\(\d{1,3}\) ([\s\S]*?)(\.END|\(\d{1,3}\))

Input text:

(1) some text some text
    some text some text
    some text some text  
(2) some text some textsome text

(3) some textsome text
    some textsome textsome text
(4) some text
.END

first group should match number (with brackets) and second group should match corresponded text.

Brad Mace
  • 27,194
  • 17
  • 102
  • 148
shivesh
  • 631
  • 2
  • 13
  • 22
  • I found out what was the cause that the regex didn't work on that site: there were some extra line breaks *after* the regex in the edit form. – True Soft Jun 18 '10 at 10:35

2 Answers2

1

Add a positive lookahead:

\(\d{1,3}\) ([\s\S]*?)(?=(\.END|\(\d{1,3}\)))
True Soft
  • 8,675
  • 6
  • 54
  • 83
  • Yes, I did. Sorround the whole expression in parenthesis: (...) – True Soft Jun 18 '10 at 09:04
  • http://www.myregextester.com/?r=0ffe51a8 this is your expression... when I click submit it doesn't match – shivesh Jun 18 '10 at 09:14
  • It is very strange. Maybe there were some options checked somehow. I modified your first demo link and this works: http://www.myregextester.com/?r=a8cd02f0 – True Soft Jun 18 '10 at 09:31
  • Okay, I also see what you mean now. That is strange. I did exactly the same. Does it work for your real app? – Lunivore Jun 18 '10 at 09:40
  • Do you mean if it works in the website in my browser? It does. You have to test it in your app, where you need it.. Edit: Sorry, I thought you were shivesh. – True Soft Jun 18 '10 at 09:45
  • please save your work at the demo site and paste the link here – shivesh Jun 18 '10 at 09:57
  • worked, don't know why it didn't work for the first time, any way thank you – shivesh Jun 18 '10 at 10:09
1

Does this do what you want?

\(\d{1,3}\) ([\s\S]*?)(?=(\.END|\(\d{1,3}\)))

Just added the "look ahead" - I'm pretty new to regexp but this other thread seemed to help:

Overlapping matches in Regex

(Thanks for the demo site by the way - not seen that before!)

Community
  • 1
  • 1
Lunivore
  • 17,277
  • 4
  • 47
  • 92
  • Haha! Someone who is more experienced than I am obviously got there first. Still, nice to know that it works. – Lunivore Jun 18 '10 at 08:13
  • Yes, I did. I saw that it did what I thought you wanted; just checking that it was what you wanted. Very cool site; bookmarked for the future! – Lunivore Jun 18 '10 at 08:53
  • strange, i try your regex with my example and it DOES NOT match anything – shivesh Jun 18 '10 at 09:15
  • Well... it's exactly the same as True Soft's below, so I guess it works for him too. I followed your demo link, pasted in the expression. It gives me 3 groups captured, with 1 matching numbers, 2 matching text and 3 empty. Looks quite pretty with the yellow and green. (Did you mean "not" instead of "now" in your first comment? If not, this conversation is confusing... which it is anyway, since it works for me...) – Lunivore Jun 18 '10 at 09:35
  • can you please save your work and that site and paste here the link? – shivesh Jun 18 '10 at 09:56
  • tank you. but i can't accept 2 answers so i accept the first one. – shivesh Jun 18 '10 at 10:09