0

我正在处理我的网站并希望更改页面上的一些内容。

我想替换以下字符串:

in the
<SPAN class="Bold">
More...
</SPAN>
column to your right.

有时是没有<span>标签:

in the
More...
column to your right.

我想用“下面”替换它。我尝试在 python 中使用简单的 replace() 来执行此操作,但因为有时文本没有<span>标签并且位于多行上,它似乎不起作用。我唯一的想法是使用正则表达式,但我没有跟上正则表达式的速度,有人可以帮忙吗?

谢谢

伊夫

4

2 回答 2

2

假设您在字符串“foo”中有 html 文本,在 Python 中执行此操作的代码如下:

import re
#re.DOTALL is used to make the . match all characters including newline
regexp = re.compile('in the.*?More\.\.\..*?column to your right\.', re.DOTALL)
re.sub(regexp, 'below', foo)
于 2009-06-26T15:54:14.307 回答
0

尝试这个:

import re
pattern = re.compile('(?:<SPAN class="Bold">\s*)?More\.\.\.(?:\s*</SPAN>)?')
str = re.sub(pattern, 'below', str)

(?:…)语法是一个非捕获分组,不能作为反向引用引用。

于 2009-06-30T11:49:53.733 回答