我正在处理一些客户意见,其中一些人没有遵循语法规则。例如,以下文本中的示例(Such as s and b.)
提供了对前一句的更多解释,其中包含两个点。
text = "I was initially scared of ANY drug after my experience. But about a year later I tried. (Such as s and b.). I had a very bad reaction to this."
首先,我想找到. (Such as s and b.).
,然后将(Such as s and b.)
之前的点替换为空格。这是我的代码,但它不起作用。
text = re.sub (r'(\.)(\s+?\(.+\)\s*\.)', r' \2 ', text )
输出应为:
"I was initially scared of ANY drug after my experience. But about a year later I tried (Such as s and b.). I had a very bad reaction to this."
我正在使用python。
答案 0 :(得分:1)
提供的样本没有多大意义,因为唯一的变化是`字符向左移动了一个位置。
然而,这可能会成功(将点保持在paranthesis内):
text = re.sub(r'\.\s*\)\s*\.', '.)', text)
或者将它放在外面:
text = re.sub(r'\.\s*\)\s*\.', ').', text)
编辑或者您可能正在寻找这个来替换开头的paranthesis之前的点?
text = re.sub(r'\.(?=\s*\(.*?\)\.)', ').', text)
答案 1 :(得分:1)