Python文档说:
re.MULTILINE: When specified, the pattern character '^' matches at the beginning of the string and at the beginning of each line (immediately following each newline)... By default, '^' matches only at the beginning of the string...
所以当我得到以下意想不到的结果时发生了什么?
>>> import re
>>> s = """// The quick brown fox.
... // Jumped over the lazy dog."""
>>> re.sub('^//', '', s, re.MULTILINE)
' The quick brown fox.\n// Jumped over the lazy dog.'
^{} 的完整定义是:
这意味着,如果告诉Python参数是什么,那么就可以传递
flags
,而不传递count
:或者,更简洁地说:
看看^{} 的定义:
第四个参数是count,您使用的是
re.MULTILINE
(即8)作为count,而不是标志。请使用命名参数:
或者先编译regex:
相关问题 更多 >
编程相关推荐