【发布时间】:2017-03-31 03:38:02
【问题描述】:
给定字符串
word = "These"
包含元组
pair = ("h", "e")
目的是替换word,使其拆分除pair 元组以外的所有字符,即输出:
('T', 'he', 's', 'e')
我试过了:
word = 'These'
pair = ('h', 'e')
first, second = pair
pair_str = ''.join(pair)
pair_str = pair_str.replace('\\','\\\\')
pattern = re.compile(r'(?<!\S)' + re.escape(first + ' ' + second) + r'(?!\S)')
new_word = ' '.join(word)
new_word = pattern.sub(pair_str, new_word)
result = tuple(new_word.split())
请注意,有时pair 元组可以包含斜杠、反斜杠或任何其他转义字符,因此上述正则表达式中的替换和转义。
有没有更简单的方法来实现相同的字符串替换?
已编辑
来自 cmets 的详细信息:
这对中的两个字符是唯一的还是不唯一的之间有区别吗?
不,应该以同样的方式对待他们。
【问题讨论】:
-
您不需要这样做
pair_str.replace('\\','\\\\'),因为re.escape会这样做。 -
first和second是什么? -
注意,我使用的是
from __future__ import unicode_literals。 -
如果代码有效并且您正在寻求如何重新设计它的建议,CodeReview.stackexchange.com 是合适的地方。 SO 是为了帮助修复根本不起作用的代码。
-
为什么会是
['T', 'he', 's', 'e', 'aa']?对只有('h', 'e'),所以应该是['T', 'he', 's', 'e', 'a', 'a']
标签: python regex string preserve