简体   繁体   English

Python-不匹配的正则表达式

[英]Python - Non-matching regex

I have the following regex: 我有以下正则表达式:

regex = compile("((?P<lastyear>[\dBFUPR]+)/)*((?P<lastseason>[\dBFUPR]+))*(^|-(?P<thisseason>[\dBFUPR]*))")

Which I am using to process horce racing form strings . 我用来处理竞速比赛形式的字符串 Sometimes a horses' form will look like this "1234-" meaning that it has not raced yet this season (there are no numbers to the right of the "-"). 有时,马匹的形态看起来像是“ 1234-”,这意味着本赛季尚未参加比赛(“-”右边没有数字)。

Currently, my regex will match "" at the end of such a form string in the thisseason group. 目前,我的正则表达式将在thisseason组中此类字符串的末尾匹配“”。 I do not want this behaviour. 我不要这种行为。 I want the group to be None in such a case. 在这种情况下,我希望该组为None ie

match = regex.match("1234-")
print match.group("thisseason") #None

Examples 例子

string = "1234/123-12"
match.group("lastyear") #1234
match.group("lastseason") #123
match.group("thisseason") #12

string = "00999F"
match.group("lastyear") #None
match.group("lastseason") #None
match.group("thisseason") #00999F

string = "12-3456"
match.group("lastyear") #None
match.group("lastseason") #12
match.group("thisseason") #3456

This works: 这有效:

>>> regex = re.compile(r'(?:(?P<lastyear>[\dBFUPR]+)/)?(?:(?P<lastseason>[\dBFUPR]+)-)?(?P<thisseason>[\dBFUPR]+)?')
>>> regex.match("1234/123-12").groupdict()
{'thisseason': '12', 'lastyear': '1234', 'lastseason': '123'}
>>> regex.match("00999F").groupdict()
{'thisseason': '00999F', 'lastyear': None, 'lastseason': None}
>>> regex.match("12-").groupdict()
{'thisseason': None, 'lastyear': None, 'lastseason': '12'}
>>> regex.match("12-3456").groupdict()
{'thisseason': '3456', 'lastyear': None, 'lastseason': '12'}

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

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