Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/355.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python被%NumberNumber";_Python_Regex_String_Split - Fatal编程技术网

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