0

如何使用 python 正则表达式将前导和尾随的任意引号字符串分别替换为任意字符串?

示例输入字符串

This is a "quote" and here's another "quote"

或者

This is a “quote&rdquo" and here's another “quote”

示例输出字符串

This is a “quote” and here's another “quote”

或者

This is a <span>"quote"</span> and here's another <span>"quote"</span>
4

4 回答 4

2

这是处理任意引号对并将它们转换为“文本”的答案的变体,即输出 2——所有这些都只需一次调用re.sub

quotes = [('"', '"'), ("&ldquot;", "&rdquot;")]
left = '|'.join(re.escape(t[0]) for t in quotes)
right = '|'.join(re.escape(t[1]) for t in quotes)
regex = r'((%s)(.*?)(%s))' % (left, right)
outstr = re.sub(regex, r'<span>"\3"</span>', instr)

测试输入字符串:

>>> replace = lambda x: re.sub(regex, r'<span>"\3"</span>', x)
>>> replace('''This is a "quote" and here's another "quote"''')
'This is a <span>"quote"</span> and here\'s another <span>"quote"</span>'
>>> replace('''This is a &ldquot;quote&rdquot; and here's another &ldquot;quote&rdquot;''')
'This is a <span>"quote"</span> and here\'s another <span>"quote"</span>'
于 2012-11-02T16:45:06.547 回答
0

我写了以下非正则表达式解决方案,但可能有更好的方法?

def replace_quotes(value, leadqt='"', tailqt='"', leadrep='<span>', tailrep='</span>', inc=True):
    while leadqt in value:
        value = value.replace(leadqt, leadrep, 1).replace(tailqt,tailrep,1)
    if inc:
        value = value.replace(leadrep, '%s%s' % (leadrep, leadqt)).replace(tailrep, '%s%s' % (tailqt, tailrep))
    return value

测试它...

>>> MYSTR = "This is a \"quote\" and here's another \"quote\""
>>> replace_quotes(MYSTR)
u'This is a <span>"quote"</span> and here\'s another <span>"quote"</span>'
于 2012-11-02T16:42:01.440 回答
0

这不适用于嵌套引号,但是:

s = 'This is a "quote" and here\'s another "quote"'
re.sub(r'"(.*?)"', r'<span>\1</span>', s)
# "This is a <span>quote</span> and here's another <span>quote</span>"

然后像这样包装:

def rep_quote(s, begin, end):
    return re.sub(r'"(.*?)"', r'{}\1{}'.format(re.escape(begin), re.escape(end)), s)
于 2012-11-02T16:47:34.140 回答
0

像这样的东西:

>>> st='''This is a "quote" and here's another "quote"'''
>>> words=re.findall(r'"\w+"',st)
>>> for x in set(words):
...     st=st.replace(x,'<span>'+x+'</span>')
... 

>>> print st
This is a <span>"quote"</span> and here's another <span>"quote"</span>
于 2012-11-02T16:47:47.473 回答