• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
regular expression

can anyone tell me what will be the regular expression of the follwoing:

yyyy-mm-dd(space)hr:iConfused e.g., exactly this format: 2001-07-31 22:05:00

I hav eto put this reg expression in 'regex'

^[0-9]{4}\-[0-9]{2}\-[0-9]{2} [0-9]{2}:[0-9]{2}:[0-9]{2}$

If you need not accept >24h
[0-9]{4}\-[0-9]{2}\-[0-9]{2} [0-2]{2}:[0-9]{2}:[0-9]{2}

Yeah, you could get really specific as far as what numbers are available in each column if you want. My version will only check that it is sets of numbers in that specific style. This would be accepted:
9999-99-99 99:99:99
0000-00-00 00:00:00
So you definitely want your code to validate the end result in addition to the regular expression in this case.

ClaudioX, you were a hair off with that code. With that regex, the hours section would only be able to accept up to 22, not 24.

Oops, thats true.. my bad.

Probably, this is right:

^[0-9]{4}\-(02-([0-2][0-9])|(((0[469]|11)-((0[1-9]|[12][0-9])|(30)))|((0[13578]|1[02])-(0[1-9]|[12][0-9]|3[01])))) (((2[0-3])|([0-1][0-9])):(([0-5][0-9])):([0-5][0-9]))$

ClaudioX, I see that you added a lot more validation as far as date and time ranges. Ignoring some of the small bugs still in it, I wouldn't recommend such a regex when it comes to validating dates, because 99% of the time you'll still need to verify things like if the date is within a set timeframe, if it actually is a leap year so February 29th is acceptable, etc. I realize mine is incredibly basic and only verifies the number pattern, but I think because of the other sanitizing that needs to be done, that's the most you'd want to validate against when it comes to user input.

I might spruce up your regex a bit and post it here regardless, just as like a tutorial or "in case you want it" type thing. If you'd like to know how you can fix your regex a bit, just let me know and I'd be happy to share some suggestions.

Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  

  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2019 MyBB Group.