如何在python中从标点符号之前而不是之后去除空格 [英] How to strip whitespace from before but not after punctuation in python

查看:94
本文介绍了如何在python中从标点符号之前而不是之后去除空格的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这里是相关的python新手.我有一个无法修改的程序的文本字符串输出.为了讨论,让我们说:

text = "这个文本.是为了测试.它是如何工作的!会吗!或者不会吗?嗯?"

我想删除标点符号前的空格,但不删除第二个空格.我一直在尝试用正则表达式来做,我知道我可以匹配我想要使用的实例match='\s[\?.!\"]\s' 作为我的搜索词.

x=re.search('\s[\?\.\!\"]\s',text)

有没有办法用 re.sub 替换搜索词并删除前导空格?关于如何进行的任何想法?

解决方案

在要保留的文本周围放置一个组,并在替换模式中按编号引用该组:

re.sub(r'\s([?.!"](?:\s|$))', r'\1', text)

请注意,我使用了 r'' 原始字符串以避免使用太多反斜杠;但是,您不需要添加这么多.

我还调整了以下空间的匹配;它现在匹配一个空格或字符串的结尾.

演示:

<预><代码>>>>进口重新>>>" text = "这个文本.是为了测试.它是如何工作的!会不会!或者不会?嗯?>>>re.sub(r'\s([?.!"](?:\s|$))', r'\1', text)这个文本.是为了测试.它是如何工作的!会不会!或者不会?嗯?"

relative python newbie here. I have a text string output from a program I can't modify. For discussion lets say:

text = "This text . Is to test . How it works ! Will it! Or won't it ? Hmm ?"

I want to remove the space before the punctuation, but not remove the second space. I've been trying to do it with regex, and I know that I can match the instances I want using match='\s[\?.!\"]\s' as my search term.

x=re.search('\s[\?\.\!\"]\s',text)

Is there a way with a re.sub to replace the search term with the leading whitespace removed? Any ideas on how to proceed?

解决方案

Put a group around the text you want to keep and refer to that group by number in the replacement pattern:

re.sub(r'\s([?.!"](?:\s|$))', r'\1', text)

Note that I used a r'' raw string to avoid having to use too many backslashes; you didn't need to add quite so many, however.

I also adjusted the match for the following space; it now matches either a space or the end of the string.

Demo:

>>> import re
>>> text = "This text . Is to test . How it works ! Will it! Or won't it ? Hmm ?"
>>> re.sub(r'\s([?.!"](?:\s|$))', r'\1', text)
"This text. Is to test. How it works! Will it! Or won't it? Hmm?"

这篇关于如何在python中从标点符号之前而不是之后去除空格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆