Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/regex/20.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 如何从字符串中删除偏执和字符串_Python_Regex_Python 3.x_String_Split - Fatal编程技术网

Python 如何从字符串中删除偏执和字符串

Python 如何从字符串中删除偏执和字符串,python,regex,python-3.x,string,split,Python,Regex,Python 3.x,String,Split,从字面上说,我一直在试图解决这个问题,但似乎我对正则表达式很差 我需要从列表中的字符串中删除WindowsPath和 x= ["(WindowsPath('D:/test/1_birds_bp.png'),WindowsPath('D:/test/1_eagle_mp.png'))", "(WindowsPath('D:/test/2_reptiles_bp.png'),WindowsPath('D:/test/2_crocodile_mp.png'))"] 所以我试过了 impo

从字面上说,我一直在试图解决这个问题,但似乎我对正则表达式很差

我需要从列表中的字符串中删除WindowsPath和

     x= ["(WindowsPath('D:/test/1_birds_bp.png'),WindowsPath('D:/test/1_eagle_mp.png'))", "(WindowsPath('D:/test/2_reptiles_bp.png'),WindowsPath('D:/test/2_crocodile_mp.png'))"]
所以我试过了

 import re
 cleaned_x = [re.sub("(?<=WindowsPath\(').*?(?='\))",'',a) for a in x]
我需要的是

cleaned_x= [('D:/test/1_birds_bp.png','D:/test/1_eagle_mp.png'), ('D:/test/2_reptiles_bp.png','D:/test/2_crocodile_mp.png')]

基本上是列表中的元组。

您可以通过如下方式使用re.findall来实现这一点:

>>> cleaned_x = [tuple(re.findall(r"[A-Z]:/[^']+", a)) for a in x]
>>> cleaned_x
[('D:/test/1_birds_bp.png', 'D:/test/1_eagle_mp.png'), ('D:/test/2_reptiles_bp.png', 
'D:/test/2_crocodile_mp.png')]
>>> 

希望能有所帮助。

您可以通过使用re.findall来实现这一点,如下所示:

>>> cleaned_x = [tuple(re.findall(r"[A-Z]:/[^']+", a)) for a in x]
>>> cleaned_x
[('D:/test/1_birds_bp.png', 'D:/test/1_eagle_mp.png'), ('D:/test/2_reptiles_bp.png', 
'D:/test/2_crocodile_mp.png')]
>>> 

希望能有所帮助。

也许你可以使用捕获组?例如:

进口稀土 re\u winpath=re.compiler'^\WindowsPath\\\'.\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\ def提取_对: m=re_winpath.matchs 如果m为无: raise VALUERRORFCANNOT从字符串中提取对:{s} 返回m组 pairs=listmapextract\u对,x
也许你可以使用捕获组?例如:

进口稀土 re\u winpath=re.compiler'^\WindowsPath\\\'.\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\ def提取_对: m=re_winpath.matchs 如果m为无: raise VALUERRORFCANNOT从字符串中提取对:{s} 返回m组 pairs=listmapextract\u对,x 这是我的看法

不太好,我分两步完成了,这样就不会生成regexp-spagetti,如果您愿意,您可以将其转换为列表理解,但它应该可以工作

for a in x:
    a = re.sub('(\()?WindowsPath', '', a)
    a = re.sub('\)$','', a)
    print(a)
这是我的看法

不太好,我分两步完成了,这样就不会生成regexp-spagetti,如果您愿意,您可以将其转换为列表理解,但它应该可以工作

for a in x:
    a = re.sub('(\()?WindowsPath', '', a)
    a = re.sub('\)$','', a)
    print(a)

为什么在每个字符串都以WindowsPath开头时使用re。。。使用切片不是很容易吗?只是好奇-如果您只想删除它们,并且它们是您想要删除的固定子字符串,那么是否需要使用regexp?你不能使用标准的字符串替换函数吗?@Richard任何能给出预期结果的函数都适用于我;。。。实际上我又看了一遍,我想你确实需要使用regexp。。。等一下:D@Richard你们真是帮了大忙!谢谢你的时间和努力!这是一个很棒的社区!为什么在每个字符串都以WindowsPath开头时使用re。。。使用切片不是很容易吗?只是好奇-如果您只想删除它们,并且它们是您想要删除的固定子字符串,那么是否需要使用regexp?你不能使用标准的字符串替换函数吗?@Richard任何能给出预期结果的函数都适用于我;。。。实际上我又看了一遍,我想你确实需要使用regexp。。。等一下:D@Richard你们真是帮了大忙!谢谢你的时间和努力!这是一个很棒的社区!谢谢!真的很欣赏这个流程!谢谢!真的很欣赏这个流程!