有效年份范围的正则表达式

目的:

Hi there, I'm trying to create a regular expression for the dash_core_components.Input "pattern" input property to validate year range inputs.

我有下面的Python v3.6.3代码,该代码提供用于输入格式验证的正则表达式(每位有效...):

import datetime as dt

full_year_   = dt.datetime.now().year
mill_        = str(full_year_)[0]
century_     = str(full_year_)[1]
decade_      = str(int(str(full_year_)[2]))
prev_decade_ = str(int(str(full_year_)[2]) - 1)
year_        = str(full_year_)[3]

prior_decades  = f'{mill_}{century_}[0-{prev_decade_}][0-9]'
current_decade = f'{mill_}{century_}{decade_}[0-{year_}]'


def century():
    return f'^({prior_decades})|({current_decade})$'


def century_set():
    # Acceptable Examples: 2018, 2019, 2020 | 2018:2020
    return f'^\s*({prior_decades}|{current_decade})([,\s]+{prior_decades}|[,\s]+{current_decade}|[,\s]*)*$|'\
           f'^\s*({prior_decades}|{current_decade})(\s*:\s*{prior_decades}|\s*:\s*{current_decade}|\s*)?$'

问题陈述:

What I'd like to implement in century_set(), if possible, is the ability to use a regular expression to verify if a range is valid or not.

换一种说法:

“ 2018:2020” >>有效“ 2020:2018” >>无效
评论