tags:

views:

27

answers:

2

Hi. I have a string like this:

BEGIN
blabla..
END;

BEGIN
one two    
END;

And I want to make regex that will find this parts separately. I made regex (?i)BEGIN[\s\S]*(?i)END; But it finds this string as one instance matching. But I need two. How to make it search END; for first occurency, not last ?

thanks!

+4  A: 
(?i)BEGIN[\s\S]*?(?i)END;

it's called lazy quantifier.

SilentGhost
great ! thanks for fast reply
Konoplianko
+2  A: 

You need a non-greedy *:

(?i)BEGIN[\s\S]*?(?i)END;

Note that regex cannot deal with nested BEGIN/END pairs. Better use a parser.

KennyTM
actually i made it for parsing PL/SQL and i hope it doesn't supports nesting :)
Konoplianko