我有一个包含两个短语的字符串,在同一字符串中由一个大写单词分隔:
c="Text is here. TEST . More text here also"
我想把两个短语分开,去掉大写单词TEST
,这样输出看起来像:
["Text is here.","More text here also"]
我做了什么:
import re
c="Text is here. TEST . More text here also"
s=re.split('[A-Z][A-Zd]+',c)
t=[re.sub('[^A-Za-z0-9]',' ',i) for i in s]
但是我仍然得到一些不需要的空格:
['Text is here ', ' More text here also']
是否有一种更简洁的python方式来生成t
?
>>> re.split('s*[A-Z]{2,}[s.]*', c)
['Text is here.', 'More text here also']
空格(可选)后接至少两个大写字符,后接空格或点(可选)。
这可以工作,但不是那么优雅。
c="Text is here. TEST . More text here also"
In [20]: [i.strip().replace('. ','') for i in c.split('TEST')]
Out[20]: ['Text is here.', 'More text here also']