简体   繁体   English

查找大于或等于 2014 的正则表达式

[英]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])$

DEMO演示

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) (这可能不是最酷的解决方案,但很容易理解)

  1. Match any numbers with 5 or more digits (>=10000) (not strictly needed for your problem description, but…)匹配任何具有 5 位或更多位数字 (>=10000) 的数字(对于您的问题描述来说不是必需的,但是……)
  2. Match any 4-digit number of the form [3-9]xxx (3000-9999)匹配任何形式为 [3-9]xxx (3000-9999) 的 4 位数字
  3. Match any 4-digit number of the form 2[1-9]xx (2100-2999)匹配任何形式为 2[1-9]xx (2100-2999) 的 4 位数字
  4. Match any 4-digit number of the form 20[2-9]x (2020-2099)匹配任何形式为 20[2-9]x (2020-2099) 的 4 位数字
  5. Match any 4-digit number of the form 201[4-9] (2015-2019)匹配表格 201[4-9] (2015-2019) 的任何 4 位数字
  6. Take the regexes you formed for steps 0-4 and OR them together so you match on any of these patterns.将您在步骤 0-4 中形成的正则表达式和它们组合在一起,以便匹配这些模式中的任何一个。

(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.

相关问题 百分比大于0且小于或等于100的正则表达式 - Regular expression for percentages greater than 0 and less than or equal to 100 使用正则表达式验证数字大于0且小于1999 - Regular expression to validate number greater than 0 and less than 1999 大于或不等于 - Greater than or not equal to 允许小于和大于符号,但不允许使用正则表达式的HTML Tag - allow less than and greater than sign but not HTML Tag using regular expression 使用 javascript 或 Jquery 或正则表达式验证器限制文本框中的小于 &lt; 和大于 &gt; 字符 - Restrict Less than < and Greater than > characters in textbox using javascript or Jquery or Regular Expression validator (大于和小于)或相同的严格形式 - (greater than and lower than) or equal strict forms 寻找正则表达式以验证密码,长度必须大于8个字符,并且至少包含一个特殊字符 - looking for a regular expression to validate a password, must be greater than 8 characters long and contain at least one special character Javascript正则表达式匹配数字零或大于零的任何整数 - Javascript regular expression to match number Zero OR any whole number greater than zero 查找数组中有多少项大于等于或小于给定数字 Javascript - Find how many items in array are greater equal or less than given number Javascript Jquery Datepicker ToDate大于或等于FromDate - Jquery Datepicker ToDate Greater than Or Equal To FromDate
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM