(?m)
is the modifier for multi-line mode. It makes ^
and $
match the beginning and end of a line, respectively, instead of matching the beginning and end of the input.
For example, given the input:
ABC DEF
GHI
The regex ^[A-Z]{3}
will match:
"ABC"
Meanwhile, the regex (?m)^[A-Z]{3}
will match:
"ABC"
"GHI"
(?s)
is the modifier for single-line mode. It adds linebreaks and newlines to the list of characters that .
will match.
Given the same input as before, the regex [A-Z]{3}.
will match (note the inclusion of the space character):
"ABC "
While the regex (?s)[A-Z]{3}.
will match:
"ABC "
"DEF
"
Despite their names, the two modes aren't necessarily mutually exclusive. In some implementations they cancel out, but, for the most part, they can be used in concert. You can use both at once by writing (?m)(?s)
or, in shorter form, (?ms)
.
EDIT:
There are certain situations where you might want to use (?ms)
. The following examples are a bit contrived, but I think they serve our purpose. Given the input (note the space after "ABC"):
ABC
DEF
GHI
The regex (?ms)^[A-Z]{3}.
matches:
"ABC "
"DEF
"
While both (?m)^[A-Z]{3}.
and (?s)^[A-Z]{3}.
match:
"ABC "
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…