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)
开发者_JS百科
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
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)
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.
.*?
finds the shortest match. Try just .*
instead.
精彩评论