Hei !
can regex support 2011999
So we “force” the first 4 digits to be 2011 or 2012 ,2013 etc, and as a set of numbers behind it? Which means that it will be 2011 + 3 digits
the first digit must always be 2011 or 2012 etc?
How can I get this using regular expression?
It depends on the values you want. It’s often better with these sort of questions to specify the rules rather than examples, since there may be many different ways to interpret the examples.
As a start, if you want the first four digits to be 201x where
xis 1 thru 9 inclusive, followed by three digits, you could use something like:(depending on the modern-ness of your regex engine.
Or, if you’re processing things like
YYYYDDDwhereDDDis the zero-based day of the year and you don’t want any dates before 2011, you may end up with a monstrosity like:(though only up to the year 2999 and allowing 366 days even in non-leap years) at which point you may want to consider just using (more readable) code rather than a regex 🙂
That’s the sort of range of possibilities you’re looking at with a question along the lines of “must always be 2011 or 2012 etc” though, if you can lock down the specifications a little more, we can hopefully remove the more bizarre possibilities.