Concise answer
If you need to check if a string has only one character (any, incl. newline), use
/^.\z/s
Explanation
The problem stems from the fact that you are using the $
without D
modifier, meaning that $
matches at the end, but not at the very end of the string. Here, $
= \Z
.
By default, $
will match the end of string and the position before the last newline. Thus, a\n
passes the if ($test =~ /^.$/ )
test, but \na
will not, since .
cannot match a newline, and it is not at the end, but at the start (it won't be matched with if ($test =~ /^.$/ )
nor with if ($test =~ /^.$/s )
).
Note that you can use \z
anchor that will force the regex engine to match at the very end of the string. Then, both test cases even with a DOTALL modifier will fail. Use /^.\z/
if you need that behavior. Or /^.\z/s
to also match a single newline.
Also, see Whats the difference between \z and \Z in a regular expression and when and how do I use it?