简体   繁体   English

Python正则表达式找不到匹配项

[英]Python regex finding no matches

I have the following file which I am trying to extract the date from 20131013.csv 我有以下文件,我正在尝试从20131013.csv提取日期

Using the regex (?P<yyyy>\\\\d{4})(?P<mm>\\\\d{2})(?P<dd>\\\\d{2}) 使用正则表达式(?P<yyyy>\\\\d{4})(?P<mm>\\\\d{2})(?P<dd>\\\\d{2})

def getEffectiveDate(regex, fileName) :
    parts = 'yyyy-mm-dd'
    formatString = '%s-%s-%s 00:00:00'
    regexGroups = parts.split("-")
    m = re.search(regex, fileName)
    if m :
        vals = []
        for regexGroup in regexGroups :
            vals.append(m.group(regexGroup))
        value = formatString%tuple(vals)
    return value

when I pass in getEffectiveDate('(?P<yyyy>\\\\d{4})(?P<mm>\\\\d{2})(?P<dd>\\\\d{2})', '20131013.csv') I don't find any matches. 当我传入getEffectiveDate('(?P<yyyy>\\\\d{4})(?P<mm>\\\\d{2})(?P<dd>\\\\d{2})', '20131013.csv')我找不到任何匹配项。

However the regex (?P<yyyy>\\\\d{4})-(?P<mm>\\\\d{2})-(?P<dd>\\\\d{2}) will find a match on 2013-10-13.csv 但是正则表达式(?P<yyyy>\\\\d{4})-(?P<mm>\\\\d{2})-(?P<dd>\\\\d{2})将在2013-10-13.csv找到匹配项2013-10-13.csv

I am wondering if this is being caused by there being nothing inbetween the regex groups and if there is anything I can do to fix this? 我想知道这是否是由正则表达式组之间没有任何关系引起的,是否有什么我可以解决的?

EDIT: 编辑:

I found a solution by doing regex.replace('\\\\\\\\', '\\\\') 我通过做regex.replace('\\\\\\\\', '\\\\')找到了解决方案

The double backslashes were causing an issue with matching. 双反斜杠引起匹配问题。 I had only tried the regex (?P<yyyy>\\\\d{4})-(?P<mm>\\\\d{2})-(?P<dd>\\\\d{2}) as a raw string so it was working in that case. 我只尝试过正则表达式(?P<yyyy>\\\\d{4})-(?P<mm>\\\\d{2})-(?P<dd>\\\\d{2})字符串,因此在这种情况下可以正常工作。

Your code works for me. 您的代码对我有用。 And it's better to define the regex as raw string. 并且最好将正则表达式定义为原始字符串。

>>> re.search(r'(?P<yyyy>\d{4})(?P<mm>\d{2})(?P<dd>\d{2})', '20131013.csv')
<_sre.SRE_Match object; span=(0, 8), match='20131013'>
>>> re.search(r'(?P<yyyy>\d{4})(?P<mm>\d{2})(?P<dd>\d{2})', '20131013.csv').group(1)
'2013'
>>> re.search(r'(?P<yyyy>\d{4})(?P<mm>\d{2})(?P<dd>\d{2})', '20131013.csv').group(2)
'10'
>>> re.search(r'(?P<yyyy>\d{4})(?P<mm>\d{2})(?P<dd>\d{2})', '20131013.csv').group(3)
'13'

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM