[英]regular expression to find greater than or equal to 2014
I want to find the years (4 digit nos) which are greater than or equal to 2014 using regular expression.我想使用正则表达式找到大于或等于 2014 的年份(4 位数字)。 [2014 - 9999]
[2014 - 9999]
I've tried this, but its not matching if the last digit is less than 4.我试过这个,但如果最后一位数字小于 4,它就不匹配。
^[2-9]\d{1,}[1-9]0*[4-9]0*$
Eg: 3013例如:3013
Please help.请帮忙。
Try the below regex to match the years from 2014 to 9999,尝试下面的正则表达式来匹配从 2014 年到 9999 年的年份,
^(?:20(?:1[4-9]|[2-9][0-9])|2[1-9][0-9][0-9]|[3-9][0-9][0-9][0-9])$
Explanation:解释:
20(?:1[4-9]|[2-9][0-9])
Matches the years from 2014 to 2099. 20(?:1[4-9]|[2-9][0-9])
匹配从 2014 年到 2099 年的年份。2[1-9][0-9][0-9]
Matches the years from 2100 to 2999. 2[1-9][0-9][0-9]
匹配从 2100 年到 2999 年的年份。[3-9][0-9][0-9][0-9]
Matches the years from 3000 to 9999. [3-9][0-9][0-9][0-9]
匹配从 3000 到 9999 的年份。Obviously, regular expressions are a terrible way to do this, so I'm assuming you're doing it this way as a mental exercise, or as a homework assignment.显然,正则表达式是一种很糟糕的方法,所以我假设你这样做是作为一种心理锻炼,或者作为家庭作业。 Either way - like most programming problems, you can solve this by breaking it down into smaller, manageable chunks:
无论哪种方式 - 就像大多数编程问题一样,您可以通过将其分解为更小的、可管理的块来解决这个问题:
(This may not be the coolest solution, but it's easy to understand) (这可能不是最酷的解决方案,但很容易理解)
(See Avinash Raj's answer, for actual regexes that do this) (有关执行此操作的实际正则表达式,请参阅 Avinash Raj 的回答)
^[3-9]\d{3}|(2(0(1[4-9]|[2-9]\d)|[1-9]\d{2}))$
pass
2014
2015
3014
3000
9999
5014
fail
2013
0014
2012
1013
((?:20(?:(?:1[4-9])|(?:[2-9]\\d)))|(?:2[1-9]\\d\\d)|(?:[3-9]\\d\\d\\d))
is what you are looking for.就是你要找的。 Extra surrounding parantheses is to enable un-ambiguous capturing or back referencing.
额外的环绕括号是为了实现无歧义的捕获或反向引用。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.