The reason why /abc$/
matches both "abc
"
and "abc"
is that $
matches the location at the end of the string, or (even without /m
modifier) the position before the newline that is at the end of the string.
You need the following regex:
/abcz/
where z
is the unambiguous very end of the string, or
/abc$/D
where the /D
modifier will make $
behave the same way as z
. See PHP.NET:
The meaning of dollar can be changed so that it matches only at the very end of the string, by setting the PCRE_DOLLAR_ENDONLY
option at compile or matching time.
See the regex demo
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…