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

#1
[eluser]mehwish[/eluser]
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'

#2
[eluser]Aken[/eluser]
Code:
^[0-9]{4}\-[0-9]{2}\-[0-9]{2} [0-9]{2}:[0-9]{2}:[0-9]{2}$

#3
[eluser]ClaudioX[/eluser]
If you need not accept >24h
Code:
[0-9]{4}\-[0-9]{2}\-[0-9]{2} [0-2]{2}:[0-9]{2}:[0-9]{2}

#4
[eluser]Aken[/eluser]
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:
Code:
9999-99-99 99:99:99
0000-00-00 00:00:00
etc
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.

#5
[eluser]ClaudioX[/eluser]
Oops, thats true.. my bad.

Probably, this is right:

Code:
^[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]))$

#6
[eluser]Aken[/eluser]
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.