2017-07-02 37 views
-1

我有例如Python的分裂的 「%NumberNumber」

mystr = "Thisisaexample123%20321Lorem%29Ipsum%28Bad123Example%20Hopefully" 

以下字符串什麼,我想實現的是:

array = mystr.split("%NumberNumber") 
for unit in array: 
    print(unit) 

控制檯:

Thisisaexample123 
321Lorem 
Ipsum 
Bad123Example 
Hopefully 

嗯。 ..也許使用正則表達式?

+0

不會它是第二行的'Lorem'。在這種情況下,使用're.split(「%\ d +」,str)'(並且不要調用變量'str') –

+0

不,它應該是321Lorem,我希望它被每個百分比符號%接着是2個數字。 或者我希望所有的%NumberNumber被替換爲「;」所以我可以分裂在「;」 – Th0m45

回答

2

只是使用re.split使用%\d\d作爲拆分正則表達式。 (百分比& 2位)

import re 

print("\n".join(re.split("%\d\d","Thisisaexample123%20321Lorem%29Ipsum%28Bad123Example%20Hopefully"))) 

結果:

Thisisaexample123 
321Lorem 
Ipsum 
Bad123Example 
Hopefully 

(旁白:請不要使用str作爲變量名,因爲它是內置的字符串對象名稱)

+0

謝謝!解決了我的問題! – Th0m45

+0

如果你覺得它可以解決你的問題,你可以接受答案:http://stackoverflow.com/help/someone-answers –