Python re.sub with a flag does not replace all occurrences
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...
那么,当我得到以下意外结果时,该怎么办?
1 2 3 4 5 6
| >>> import re
>>> s ="""// The quick brown fox.
... // Jumped over the lazy dog."""
>>> re.sub('^//', '', s, re.MULTILINE)
' The quick brown fox.
// Jumped over the lazy dog.' |
查看re.sub的定义:
1
| re.sub(pattern, repl, string[, count, flags]) |
第4个参数是计数,您将re.MULTILINE(即8)用作计数,而不是作为标志。
使用命名参数:
1
| re.sub('^//', '', s, flags=re.MULTILINE) |
或先编译正则表达式:
1
| re.sub(re.compile('^//', re.MULTILINE), '', s) |
1
| re.sub('(?m)^//', '', s) |
re.sub的完整定义是:
1
| re.sub(pattern, repl, string[, count, flags]) |
这意味着,如果您告诉Python参数是什么,则可以传递flags而不传递count:
1
| re.sub('^//', '', s, flags=re.MULTILINE) |
或者,更简洁地说:
1
| re.sub('^//', '', s, flags=re.M) |
|