tags:

views:

34

answers:

3

Let's use this as sample data :

text=<<EOF
#if A==20
     int b = 20;
#else
     int c = 30;
#endif

And this code :

puts text.scan(/\#.*?\#/m)

Why is this only capturing this:

#if A==20
    int b = 20;
#

I was expecting this to match as well:

#else
    int c = 30;
#

What do I have to modify so that it captures that as well? I used /m for multiline matching, but it doesn't seem to work.

A: 

.*? finds the shortest match. Try just .* instead.

Hyman Rosen
If I don't use `?` I'll only get one match. I'd like 2.
Geo
+2  A: 

It doesn't match the second part, because the "#" before the else has already been consumed, so all that's left ist

else
    int c = 30;
#

which does not match the pattern. You can fix this by using lookahead to match the second # without consuming it:

text.scan(/#.*?(?=#)/m)
sepp2k
+1  A: 

Second # in your input was already matched by the first substring scan found. From there, it proceeds to scan the remaining part of the string, which is:

else
        int c = 30;
#endif

which of course doesn't contain anything to match your regex anymore.

Pavel Minaev