Strange regex behaviour - Printable Version +- CodeIgniter Forums (https://forum.codeigniter.com) +-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20) +--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forumdisplay.php?fid=23) +--- Thread: Strange regex behaviour (/showthread.php?tid=23288) |
Strange regex behaviour - El Forum - 10-06-2009 [eluser]alboyd[/eluser] Hi Guys, I have no idea about regex really - never really tried to figure it out too much. I can guess how the regex below is supposed to work and I think I found this snippet online somewhere. All along so far I have been testing with a time of 20:30 and it has worked but I have just found out that other valid HH:MM times are not working? Here is some testing code I am running. Code: function index() Here are the results: Code: 01:50 Anyone got an idea of what is wrong - or does anyone know how I can validate a time is HH:MM. Thanks! Strange regex behaviour - El Forum - 10-06-2009 [eluser]alboyd[/eluser] I've found this regex online and it seems to work. Code: ^(([0-1][0-9])|([2][0-3]))[:]([0-5][0-9])$ I'd still be interested in ideas of why the original was not working consistently? Strange regex behaviour - El Forum - 10-06-2009 [eluser]aquariuz[/eluser] [0-23] means: 0 to 2, plus 3. So basically it means 0 to 3. As soon as a 4 or higher appears in the first segment, the regular expression fails. Strange regex behaviour - El Forum - 10-06-2009 [eluser]Phil Sturgeon[/eluser] Actually I believe it is a 05 != 5 problem. Also, I would switch to using preg as ereg is slowly being depreciated. It will start flagging errors in PHP 6. Strange regex behaviour - El Forum - 10-06-2009 [eluser]BrianDHall[/eluser] [quote author="aquariuz" date="1254845365"][0-23] means: 0 to 2, plus 3. So basically it means 0 to 3. As soon as a 4 or higher appears in the first segment, the regular expression fails.[/quote] Just wanted to confirm this as correct and elucidate. Regex works on characters individually - to a regex there is no number greater than 9. 90 is not bigger than 9, because it is actually just a 9 and a 0 that happen to be next to each other. To get a number less than one-hundred you might try, for instance "/[0-9][0-9]/" - but again it will not behave as expected as it will match the 1 and the 0 of the number 100. A good site for regex as a reference: http://www.regular-expressions.info/ Strange regex behaviour - El Forum - 10-06-2009 [eluser]sophistry[/eluser] you could shorten the new regex by taking out a few brackets and a redundant dash... also, definitely use the preg_ functions over ereg_ functions. from: Code: ^(([0-1][0-9])|([2][0-3]))[:]([0-5][0-9])$ to: Code: ^(([01][0-9])|(2[0-3]))\:([0-5][0-9])$ |