[英]Python re.sub with a flag does not replace all occurrences
The Python docs say: 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...
re.MULTILINE:指定时,模式字符 '^' 匹配字符串开头和每行开头(紧跟在每个换行符之后)...默认情况下,'^' 仅匹配字符串开头...
So what's going on when I get the following unexpected result?那么当我得到以下意想不到的结果时发生了什么?
>>> 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.'
Look at the definition of re.sub
:看看
re.sub
的定义:
re.sub(pattern, repl, string[, count, flags])
The 4th argument is the count, you are using re.MULTILINE
(which is 8) as the count, not as a flag.第四个参数是计数,您使用
re.MULTILINE
(即 8)作为计数,而不是标志。
Either use a named argument:要么使用命名参数:
re.sub('^//', '', s, flags=re.MULTILINE)
Or compile the regex first:或者先编译正则表达式:
re.sub(re.compile('^//', re.MULTILINE), '', s)
re.sub('(?m)^//', '', s)
The full definition of re.sub
is: re.sub
的完整定义是:
re.sub(pattern, repl, string[, count, flags])
Which means that if you tell Python what the parameters are, then you can pass flags
without passing count
:这意味着如果你告诉 Python 参数是什么,那么你可以传递
flags
而不传递count
:
re.sub('^//', '', s, flags=re.MULTILINE)
or, more concisely:或者,更简洁地说:
re.sub('^//', '', s, flags=re.M)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.