我有一个字符串,如下所示 -"nnsome textt goes here. somett other text goes herebnnn"
.
我想要的 -"some text goes here. some other text goes here."
这是我正在做的:re.sub('[^A-Za-z0-9]+', ' ', s)
问题是这也删除了所有标点符号。如何保留这些内容?
这是一个解决方案,可查找字符串中的所有转义字符,然后将其删除。
r = repr(s) # Convert escape sequences to literal backslashes
r = r[1:-1] # Remove the quote characters introduced by `repr`
escapes = set(re.findall(r'\wd*', r)) # Get escape chars
answer = re.sub('|'.join(map(re.escape, escapes)), '', r) # Remove them
# ome text goes here. some other text goes here