时间:2020-11-20 18:01:36 | 栏目:Ruby | 点击:次
include Re
delim = re.any("- /.")
century_prefix = re("19") | re("20")
under_ten = re("0") + re.any("1-9")
ten_to_twelve = re("1") + re.any("012")
ten_and_under_thirty = re.any("12") + re.any("0-9")
thirties = re("3") + re.any("01")
year = (century_prefix + re.digit.repeat(2)).capture(:year)
month = (under_ten | ten_to_twelve).capture(:month)
day = (under_ten | ten_and_under_thirty | thirties).capture(:day)
date = (year + delim + month + delim + day).all
例如,构建匹配电话号码的正则表达式:
if PHONE_REGEXP =~ string
# blah blah blah
end