import re
examples = ["2023-12-25 is my favourite day of the year", "I like to eat candy", "2001- a space odyssey was released in 1968", "1969 was a great year for dad rock"]
date_pattern = r"^\d{4}-.*"
anti_date_pattern = r"^\D+.*|^\d{4}[^-].*"
start_with_year_dash = [re.findall(date_pattern, example) for example in examples if re.findall(date_pattern, example)] # [['2023-12-25 is my favourite day of the year'], ['2001- a space odyssey was released in 1968']]
dont_start_with_year_dash = [re.findall(anti_date_pattern, example) for example in examples if re.findall(anti_date_pattern, example)] # [['I like to eat candy'], ['1969 was a great year for dad rock']]
2条答案
按热度按时间wh6knrhe1#
字符串
第一个替换匹配前4个字符中有一个不是数字的字符串。第二个选择匹配4位数字,后面不跟连字符。
DEMO
tktrz96b2#
这里有两个Python的例子。一个用于匹配日期,另一个用于不匹配日期:
字符串