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

Python正则表达式日期

Python正则表达式日期,python,Python,如何对以下变量使用Python正则表达式来提取日期 a = 'abc_de_00_abcd_20130605.zip' a = 'abc_de_20130605_00_abcd.zip' 我尝试了以下方法,但不起作用 re.match(r'[0-9]{8}',a) 检查是否可以在字符串的开头找到模式(就好像您要求的是^[0-9]{8}而不是[0-9]{8}) 由于日期字符串可以位于文件名中的不同位置,您需要: re.search(r'[0-9]{8}', a) # results in

如何对以下变量使用Python正则表达式来提取日期

a = 'abc_de_00_abcd_20130605.zip'

a = 'abc_de_20130605_00_abcd.zip'
我尝试了以下方法,但不起作用

re.match(r'[0-9]{8}',a)
检查是否可以在字符串的开头找到模式(就好像您要求的是
^[0-9]{8}
而不是
[0-9]{8}

由于日期字符串可以位于文件名中的不同位置,您需要:

re.search(r'[0-9]{8}', a)  # results in a match

您需要使用重新搜索方法。re.match尝试匹配整个输入字符串。你需要的是

re.search(r'[0-9]{8}', a).group()