First of all you can simplify your regex to:
for?\(.*;.*;.*\).*\s*for?\(.*;.*;.*\)
You see ".*" means: any occurrence of any char. Adding a "?" means: at least one time. any times one time ... as said: doesn't make sense.
But beyond that: I simply advise to separate your two cases.
Beyond that: your current pattern looks for
- for construct
- whitespaces
- for construct
And as you could have noticed yourself - the multiline part is working fine with that. Your current pattern already matches two lines fine. But of course it can not match your second example. Because your second represents
- for construct
- whitespaces
- somecode
- whitespaces
- for construct
In order to account for that, lets look at this:
for?\(.*;.*;.*\).*\s*.*\s*for?\(.*;.*;.*\)
In other words: for each line between those for constructs, you need one .*\s*
SO, for me
for?\(.*;.*;.*\).*\s*.*\s*.*\s*for?\(.*;.*;.*\)
matches your both your for loop constructs.
The problem here: if "somecode" would be two lines, you need to again put another "line-content-matcher" entry into the regex.
I tried
for?\(.*;.*;.*\)(.*\s*)*for?\(.*;.*;.*\)
but at least for me, that crashes eclipse.
(which doesn't come as surprise, as this question makes it clear that "counting", aka "indefinite" structure can not work with regular expressions)