如何使用python regex分别用任意字符串替换前导和尾随任意引号字符串?
示例输入字符串
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>
答案 0 :(得分: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>'
答案 1 :(得分: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>'
答案 2 :(得分: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)
答案 3 :(得分: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>