tags:

views:

62

answers:

2

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.

+1  A: 

Add a positive lookahead:

\(\d{1,3}\) ([\s\S]*?)(?=(\.END|\(\d{1,3}\)))
True Soft
did you try it at the demo? it's now working
shivesh
Yes, I did. Sorround the whole expression in parenthesis: (...)
True Soft
http://www.myregextester.com/?r=0ffe51a8this is your expression...when I click submit it doesn't match
shivesh
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
Okay, I also see what you mean now. That is strange. I did exactly the same. Does it work for your real app?
Lunivore
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
please save your work at the demo site and paste the link here
shivesh
I did: http://www.myregextester.com/?r=a8cd02f0
True Soft
worked, don't know why it didn't work for the first time, any way thank you
shivesh
+1  A: 

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:

http://stackoverflow.com/questions/320448/overlapping-matches-in-regex

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

Lunivore
Haha! Someone who is more experienced than I am obviously got there first. Still, nice to know that it works.
Lunivore
did you try it at the demo? it's now working
shivesh
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
strange, i try your regex with my example and it DOES NOT match anything
shivesh
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
can you please save your work and that site and paste here the link?
shivesh
tank you. but i can't accept 2 answers so i accept the first one.
shivesh
Of course, as it should be! :)
Lunivore