最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - regular expression to find greater than or equal to 2014 - Stack Overflow

programmeradmin2浏览0评论

I want to find the years (4 digit nos) which are greater than or equal to 2014 using regular expression. [2014 - 9999]

I've tried this, but its not matching if the last digit is less than 4.

^[2-9]\d{1,}[1-9]0*[4-9]0*$

Eg: 3013

Please help.

I want to find the years (4 digit nos) which are greater than or equal to 2014 using regular expression. [2014 - 9999]

I've tried this, but its not matching if the last digit is less than 4.

^[2-9]\d{1,}[1-9]0*[4-9]0*$

Eg: 3013

Please help.

Share Improve this question edited Jun 9, 2021 at 5:22 The Godfather asked Jul 29, 2014 at 17:38 The GodfatherThe Godfather 92912 silver badges19 bronze badges 3
  • 5 Why do you want to use a regular expression for this? Extract the digits (with the regular expression), convert to a number, and pare to the number 2014 – Ian Commented Jul 29, 2014 at 17:40
  • 3 Uh, numb >= 2014 why would you need a regex for that ? – adeneo Commented Jul 29, 2014 at 17:40
  • @Ian I've a webservice which receives an input xml and it gets parsed inside the WS and node values get saved in DB based based on another parsing xml in WS. For manipulating the data in the i/p xml, I use regex in my parsing xml. My code will later convert the regex and input data using a custom translator and save it in DB. So if I follow what you said, based on the current methodology I use in WS, I'll end up in creating several xml nodes in the parser xml. Hope you understood my point. I'm a bit bad in explaining stuff. Sorry for that :( Hope you understood my point. – The Godfather Commented Aug 1, 2014 at 7:53
Add a ment  | 

4 Answers 4

Reset to default 5

Try the below regex to match the years from 2014 to 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.
  • 2[1-9][0-9][0-9] Matches the years from 2100 to 2999.
  • [3-9][0-9][0-9][0-9] Matches the years from 3000 to 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…)
  2. Match any 4-digit number of the form [3-9]xxx (3000-9999)
  3. Match any 4-digit number of the form 2[1-9]xx (2100-2999)
  4. Match any 4-digit number of the form 20[2-9]x (2020-2099)
  5. Match any 4-digit number of the form 201[4-9] (2015-2019)
  6. Take the regexes you formed for steps 0-4 and OR them together so you match on any of these patterns.

(See Avinash Raj's answer, for actual regexes that do this)

^[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.

发布评论

评论列表(0)

  1. 暂无评论