Don't ask to ask, just ask | |
PerlMonks |
Re^5: regex doubt on excludingby Athanasius (Archbishop) |
on Apr 22, 2014 at 09:33 UTC ( [id://1083119]=note: print w/replies, xml ) | Need Help?? |
Hello SuicideJunkie, and thanks for the answer. Unfortunately, I’m still confused. :-( From your explanation, I would expect that making the whitespace match non-greedy would prevent the intermediate newline(s) from being eliminated. But it doesn’t (see below). Here is my current understanding (obviously flawed) of what should happen:
Given these assumptions, I would expect that the regex /^\s*?$/ would match the string "a\n\n\nb" as follows: First, ^ matches after the first newline. Since \s*? is non-greedy, the regex engine looks for the shortest match satisfying \s*?$, and finds it in the zero-length string between the first two newlines. This it replaces with another zero-length string. It then starts looking for the next match with ^ matching after the second newline. Again, it finds and replaces a zero-length string. Finaly, ^ matches after the final newline, but no match is found. Result: the string is unchanged. However:
Output:
One of the newlines is being deleted, so my understanding must be wrong somewhere. I did try adding use re 'debug'; but I’m only just learning to interpret the output. I think the relevant part is:
which seems to show that the match I would expect (empty line) is rejected, but I don’t know why it is. What am I missing?
In Section
Seekers of Perl Wisdom
|
|