我一直在尝试这段代码,试图读取没有空格的文本字符串。该代码需要通过使用正则表达式标识所有大写字母来分隔字符串。但是,我似乎无法显示大写字母。
import re
mystring = 'ThisIsStringWithoutSpacesWordsTextManDogCow!'
wordList = re.sub("[^\^a-z]"," ",mystring)
print (wordList)
python大神给出的解决方案
尝试:
re.sub("([A-Z])"," \\1",mystring).split()
这会在每个大写字母前加一个空格,并在这些空格上分割。
输出:
['This',
'Is',
'String',
'Without',
'Spaces',
'Words',
'Text',
'Man',
'Dog',
'Cow!']