To match a newline, or "any symbol" without re.S
/re.DOTALL
, you may use any of the following:
[sS]
[wW]
[dD]
The main idea is that the opposite shorthand classes inside a character class match any symbol there is in the input string.
Comparing it to (.|s)
and other variations with alternation, the character class solution is much more efficient as it involves much less backtracking (when used with a *
or +
quantifier). Compare the small example: it takes (?:.|
)+
45 steps to complete, and it takes [sS]+
just 2 steps.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…