考虑以下多行字符串:
>> print s
shall i compare thee to a summer's day?
thou art more lovely and more temperate
rough winds do shake the darling buds of may,
and summer's lease hath all too short a date.
re.sub()
将所有and
替换为AND
:
>>> print re.sub("and", "AND", s)
shall i compare thee to a summer's day?
thou art more lovely AND more temperate
rough winds do shake the darling buds of may,
AND summer's lease hath all too short a date.
但是re.sub()
不允许^
锚定到该行的开头,因此添加它不会导致and
被替换:
>>> print re.sub("^and", "AND", s)
shall i compare thee to a summer's day?
thou art more lovely and more temperate
rough winds do shake the darling buds of may,
and summer's lease hath all too short a date.
如何将re.sub()
与行尾(^
)或行尾($
)锚点结合使用?
答案 0 :(得分:12)
您忘了启用多线模式。
re.sub("^and", "AND", s, flags=re.M)
re.M
re.MULTILINE
指定时,模式字符
'^'
匹配字符串的开头和每行的开头(紧跟在每个换行符之后);模式字符'$'
在字符串的末尾和每行的末尾(紧接在每个换行符之前)匹配。默认情况下,'^'
仅匹配字符串的开头,'$'
仅匹配字符串的结尾,紧接在字符串末尾的换行符(如果有)之前。
flags参数不适用于早于2.7的python;所以在这些情况下你可以直接在正则表达式中设置它,如下所示:
re.sub("(?m)^and", "AND", s)
答案 1 :(得分:7)