$ perl -MData::Dump -Mre=debug -le " dd( $_=qq{a\n\nb} ); s{^$\n?}{boop}ms; dd( $_ ); " "a\n\nb" Compiling REx "^%nn?" Final program: 1: MBOL (2) 2: EXACT <\n> (4) 4: CURLY {0,1} (8) 6: EXACT (0) 8: END (0) anchored "%n" at 0 (checking anchored) anchored(MBOL) minlen 1 Guessing start of match in sv for REx "^%nn?" against "a%n%nb" Found anchored substr "%n" at offset 1... Found /^/m, restarting lookup for check-string at offset 2... Found anchored substr "%n" at offset 2... Position at offset 2 does not contradict /^/m... Guessed: match at offset 2 Matching REx "^%nn?" against "%nb" 2 <%nb> | 1:MBOL(2) 2 <%nb> | 2:EXACT <\n>(4) 3 | 4:CURLY {0,1}(8) EXACT can match 0 times out of 1... 3 | 8: END(0) Match successful! "a\nboopb" Freeing REx: "^%nn?"