繁体   English   中英

Python单字符清除

[英]Python single character clean

我想从文本中删除所有一个字符的单词。

例如:我想清除下面文本中的所有加粗字符。 a?d*等),重新调整已清除的文本。

Lorem Ipsum仅仅假人吗? 文字| 印刷和排字行业。 Lorem存有一直是业界标准的虚拟曾文本自1500年,当一个未知的打印机了类型的厨房和炒它d键使*模式标本书。 它已存活不仅五个世纪,但也跃入[电子排版,其余基本保持不变。

使用正则表达式:

re.sub(r'((?:^|(?<=\s))\S\s|\s\S(?:$|(?=\s)))', '', inputtext)

这将删除在文本开头或前面带有空白的任何一个非空白字符,然后删除一个空白字符(也将被删除), 或者删除一个空白字符后跟一个非空白字符,该字符要么位于文本开头,要么被删除。文本末尾或后跟空白。

这样可以确保一个字符周围的空格也已正确删除。

演示:

>>> import re
>>> inputtext = '''\
... Lorem Ipsum is simply a dummy ? text | of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it d to make * type specimen book. It has survived not only five centuries, but also the leap into [ electronic typesetting, remaining essentially unchanged.
... '''
>>> re.sub(r'((?:^|(?<=\s))\S\s|\s\S(?:$|(?=\s)))', '', inputtext)
"Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took galley of type and scrambled it to make type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged.\n"

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM