Matching dates with regular expressions in Python?

后端 未结 6 1800
-上瘾入骨i
-上瘾入骨i 2020-12-31 17:56

I know that there are similar questions to mine that have been answered, but after reading through them I still don\'t have the solution I\'m looking for.

Using Pyth

6条回答
  •  我在风中等你
    2020-12-31 18:25

    A group is identified by parentheses (...) and they count from left to right, outermost first. Your final expression looks like this:

    ((19|20)[0-9][0-9])

    The outermost parentheses match the entire year, and the inside parentheses match the first two digits. Hence, for a date like "1989", the two match groups are going to be 1989 and 19. Since you don't want the inner group (first two digits), you should use a non-capturing group instead. Non-capturing groups start with ?:, used like this: (?:a|b|c)

    By the way, there is some good documentation on how to use regular expressions here.

提交回复
热议问题