本文介绍了Python re.sub() 行首锚定的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

考虑以下多行字符串:

>>印刷我可以把你比作夏日吗?你是更可爱,更温和的狂风确实撼动了五月的萌芽,夏天的租约太短了.

re.sub() 将所有出现的 and 替换为 AND:

>>>打印 re.sub("and", "AND", s)我可以把你比作夏日吗?你是更可爱,更温和的狂风确实撼动了五月的萌芽,而且夏天的租约日期太短了.

但是 re.sub() 不允许 ^ 锚定到行首,所以添加它不会导致 and> 被替换:

>>>打印 re.sub("^and", "AND", s)我可以把你比作夏日吗?你是更可爱,更温和的狂风确实撼动了五月的萌芽,夏天的租约太短了.

如何将 re.sub() 与行首 (^) 或行尾 ($) 锚点?

解决方案

您忘记启用多行模式.

re.sub("^and", "AND", s, flags=re.M)

re.M
re.MULTILINE

指定时,模式字符 '^' 匹配字符串的开头和每行的开头(紧跟在每个换行符之后);并且模式字符 '$' 在字符串的末尾和每行的末尾(紧接在每个换行符之前)匹配.默认情况下,'^' 只匹配字符串的开头,'$' 只匹配字符串的结尾和换行符(如果有)之前的位置字符串的结尾.

来源

flags 参数不适用于 2.7 以上的 Python;所以在这些情况下,你可以直接在正则表达式中设置它,如下所示:

re.sub("(?m)^and", "AND", s)

Consider the following multiline string:

>> 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() replaces all the occurrence of and with 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.

But re.sub() doesn't allow ^ anchoring to the beginning of the line, so adding it causes no occurrence of and to be replaced:

>>> 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.

How can I use re.sub() with start-of-line (^) or end-of-line ($) anchors?

解决方案

You forgot to enable multiline mode.

re.sub("^and", "AND", s, flags=re.M)

source

The flags argument isn't available for python older than 2.7; so in those cases you can set it directly in the regular expression like so:

re.sub("(?m)^and", "AND", s)

这篇关于Python re.sub() 行首锚定的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-14 20:02