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.
It doesn’t match the second part, because the “#” before the else has already been consumed, so all that’s left ist
which does not match the pattern. You can fix this by using lookahead to match the second
#without consuming it: