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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/regex/19.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 - Fatal编程技术网

Python 如何获取两个下划线之间的字符串?

Python 如何获取两个下划线之间的字符串?,python,regex,Python,Regex,我想提取示例部分和这个c584536f-ab26-40ce-b5b6-386f755ba747 输出: str = "IV_04.03.2019-10-56-45_example_c584536f-ab26-40ce-b5b6-386f755ba747_1.csv" s1= str.split("_")[2] s2= str.split("_")[3] print (s1) print (s2) 这将提取由下划线包围且内部不包含下划线的所有子字符串。最后两个下划线??因为下划线之间还有其他子字

我想提取示例部分和这个c584536f-ab26-40ce-b5b6-386f755ba747

输出:

str = "IV_04.03.2019-10-56-45_example_c584536f-ab26-40ce-b5b6-386f755ba747_1.csv"
s1= str.split("_")[2]
s2= str.split("_")[3]
print (s1)
print (s2)

这将提取由下划线包围且内部不包含下划线的所有子字符串。

最后两个下划线??因为下划线之间还有其他子字符串,所以我想提取示例和c584536f-ab26-40ce-b5b6-386f755ba747fyi:“c584536f-ab26-40ce-b5b6-386f755ba747”看起来像是使用正则表达式的教科书示例。您也需要提取04.03.2019-10-56-45吗?@Abdullah Ahsan说示例也应该被提取。收到了,谢谢救命,不客气@AbdullahAhsan
str = "IV_04.03.2019-10-56-45_example_c584536f-ab26-40ce-b5b6-386f755ba747_1.csv"
s1= str.split("_")[2]
s2= str.split("_")[3]
print (s1)
print (s2)
example
c584536f-ab26-40ce-b5b6-386f755ba747
>>> "IV_04.03.2019-10-56-45_example_c584536f-ab26-40ce-b5b6-386f755ba747_1.csv".split("_")[1:-1]
['04.03.2019-10-56-45', 'example', 'c584536f-ab26-40ce-b5b6-386f755ba747']