Python被%NumberNumber";
例如,我有以下字符串Python被%NumberNumber";,python,regex,string,split,Python,Regex,String,Split,例如,我有以下字符串 mystr = "Thisisaexample123%20321Lorem%29Ipsum%28Bad123Example%20Hopefully" 我努力实现的目标是: array = mystr.split("%NumberNumber") for unit in array: print(unit) 控制台: Thisisaexample123 321Lorem Ipsum Bad123Example Hopefully 嗯。。。也许用正则表达式 只需使
mystr = "Thisisaexample123%20321Lorem%29Ipsum%28Bad123Example%20Hopefully"
我努力实现的目标是:
array = mystr.split("%NumberNumber")
for unit in array:
print(unit)
控制台:
Thisisaexample123
321Lorem
Ipsum
Bad123Example
Hopefully
嗯。。。也许用正则表达式 只需使用
re.split
使用%\d\d
作为拆分正则表达式即可。(百分比和2位数)
结果:
Thisisaexample123
321Lorem
Ipsum
Bad123Example
Hopefully
(旁白:不要使用
str
作为变量名,因为它是内置的字符串对象名)第二行不是Lorem
。在这种情况下,使用re.split(“%\d+”,str)
(不要调用变量str
)不,它应该是321Lorem,我希望它被每个百分比符号%拆分,后面跟两个数字。或者,我希望所有%NumberNumber都替换为“;”以便我可以在“;”处拆分。如果您认为答案解决了您的问题,您可以接受该答案:
Thisisaexample123
321Lorem
Ipsum
Bad123Example
Hopefully