只保留两个特殊字符之间的字母



我试着删除除了两个字母之间的所有内容:

text = "nice :star-struck:    :face blowing a kiss: 99"

输出应该像这样:

输出:nice :starstruck: :faceblowingakiss: 99

是否有一个简单的正则表达式来做到这一点?

编辑(谢谢你Samwise &;Andrej Kesely):

text = re.sub(
r"(?<=:[a-zA-Z])(.*?)(?=:)",
lambda g: "{}".format(re.sub(r"[^a-zA-Z]", "", g.group(1))), text
)

re的解:

import re
text = "nice :star-struck:    :face blowing a kiss: 99"
# remove non-alpha chars inside : :
text = re.sub(
r":(.*?):",
lambda g: ":{}:".format(re.sub(r"[^a-zA-Z]", "", g.group(1))),
text,
)
# remove extra spaces
print(" ".join(text.split()))

打印:

nice :starstruck: :faceblowingakiss: 99

下面是使用str.split()str.join()的解决方案:

>>> text = "nice :star-struck: :face blowing a kiss: 99"
>>> ':'.join(
...     ''.join(c for c in s if c.isalpha()) if i % 2 else s
...     for i, s in enumerate(text.split(":"))
... )
'nice :starstruck: :faceblowingakiss: 99'

使用列表推导式作为一行:

>>> " ".join(x for x in "nice :star-struck:     :face blowing a kiss: 99".split(" ") if x.strip())
'nice :star-struck: :face blowing a kiss: 99'

使用列表理解和文本在一个变量中:

>>> text="nice :star-struck:     :face blowing a kiss: 99"
>>> text=" ".join(x for x in text.split(" ") if x.strip())
>>> print(text)
nice :star-struck: :face blowing a kiss: 99