文件名格式的正则表达式

问题描述 投票:0回答:2

我正在尝试创建以下字符串的python正则表达式: ABC--2000-01-10X13-11-44.237Z--572b3b7681572b3b7681572b3b7681572b3b7681 这是我管理的: ^ABC\S\S[0-9A-T\S.]{24}\S\S[a-z0-9]{40}$

问题是python一直在抱怨异常的反斜杠缺失或r前缀,我已经尝试了两个没有运气的建议。通过用\S替换-,我设法让它停止对反斜杠的呐喊,所以正则表达式看起来像这样:^ABC--[0-9A-T-.]{24}--[a-z0-9]{40}$ 但现在它不再匹配,我认为这是因为标点符号.?我不确定,我希望有人愿意帮助我解决这个问题。

python regex regex-lookarounds
2个回答
0
投票

对于你的SECOND正则表达式,实际上它不匹配,因为你已经使用了像[0-9A-T-.]{24}这样的字符范围但是在你给定的输入字符串中你有一个叫做X的字符所以我认为你需要修改你现有的正则表达式,例如A-TA-Z然后它会完美匹配你的字符串。

^ABC--[0-9A-Z-.]{24}--[a-z0-9]{40}$

REGEX:https://regex101.com/r/fsp3FS/24

Python代码:

# coding=utf8
# the above tag defines encoding for this document and is for Python 2.x compatibility

import re

regex = r"^ABC--[0-9A-Z-.]{24}--[a-z0-9]{40}$"

test_str = "ABC--2000-01-10X13-11-44.237Z--572b3b7681572b3b7681572b3b7681572b3b7681"

matches = re.search(regex, test_str, re.IGNORECASE)

if matches:
    print ("Match was found at {start}-{end}: {match}".format(start = matches.start(), end = matches.end(), match = matches.group()))

    for groupNum in range(0, len(matches.groups())):
        groupNum = groupNum + 1

        print ("Group {groupNum} found at {start}-{end}: {group}".format(groupNum = groupNum, start = matches.start(groupNum), end = matches.end(groupNum), group = matches.group(groupNum)))

0
投票

将正则表达式指定为原始字符串(r'...'):

pat = re.compile(r'^ABC\S\S[0-9A-T\S.]{24}\S\S[a-z0-9]{40}$')

否则(在“普通”字符串中)反斜杠必须加倍。

示例(工作)代码:

import re

str = 'ABC--2000-01-10X13-11-44.237Z--572b3b7681572b3b7681572b3b7681572b3b7681'
pat = re.compile(r'^ABC\S\S[0-9A-T\S.]{24}\S\S[a-z0-9]{40}$')
print(pat.match(str).group())
© www.soinside.com 2019 - 2024. All rights reserved.