sandy1028 Posted December 5, 2012 Share Posted December 5, 2012 2012.10.12 14.11.2013 Please tell me how to write the regular expression which matches both the format. I have tried the regex below but matches only forst format. ([0-9]{4}) \.([0-9]{2})\.[0-9]{2}|^([0-9]{2})\.[0-9]{2}\. \.[0-9]{2}\.([0-9]{2})|^[0-9]{2}\.([0-9]{2})\. Quote Link to comment https://forums.phpfreaks.com/topic/271627-date-format/ Share on other sites More sharing options...
Christian F. Posted December 5, 2012 Share Posted December 5, 2012 Write two RegExps, one for each format, and then join them with a pipe (|). Once that's done, add the delimiters, and you're set. Example: /\d{2}\.\d{4}|\d{4}\.\d{2}/ Quote Link to comment https://forums.phpfreaks.com/topic/271627-date-format/#findComment-1397669 Share on other sites More sharing options...
sandy1028 Posted December 5, 2012 Author Share Posted December 5, 2012 yes tried. But no luck. Verified in regexteser.com Quote Link to comment https://forums.phpfreaks.com/topic/271627-date-format/#findComment-1397674 Share on other sites More sharing options...
sandy1028 Posted December 5, 2012 Author Share Posted December 5, 2012 It doesn't take the OR condition. Please help Quote Link to comment https://forums.phpfreaks.com/topic/271627-date-format/#findComment-1397676 Share on other sites More sharing options...
Christian F. Posted December 5, 2012 Share Posted December 5, 2012 Please post the updated code of your attempt, and remember: Keep it simple. Quote Link to comment https://forums.phpfreaks.com/topic/271627-date-format/#findComment-1397680 Share on other sites More sharing options...
sandy1028 Posted December 5, 2012 Author Share Posted December 5, 2012 12.10.2012 format Parsing year - ^[0-9]{2}\.[0-9]{2}\.([0-9]{4}) Parsing Month - \.([0-9]{2})\. parsing day - ^([0-9]{2})\. 2012-11-10 Parsing year ^([0-9]{4})\. Parsing month \.([0-9]{2})\. Parsing day ^[0-9]{4}\.[0-9]{2}\.([0-9]{2}) We have a field on the page, where the page should support both the expression. or condition should be for the year ^[0-9]{2}\.[0-9]{2}\.([0-9]{4})|^([0-9]{4})\. which doesn't work. Quote Link to comment https://forums.phpfreaks.com/topic/271627-date-format/#findComment-1397688 Share on other sites More sharing options...
Christian F. Posted December 5, 2012 Share Posted December 5, 2012 (edited) Let's translate the RegExps for you, so that you can see where you went wrong. ^ # Bind to the start of the string. [0-9]{2} # Match two Digits (0-9). \. # Followed by a literal period. [0-9]{2} # Then another two digits. \. # And another literal period. ( # Open a capturing sub group, which is saved as group 1 [0-9]{4} # Then match four digits. ) # Close the sub group. So, as you can see you're matching a whole lot more than just the year here, and the same with the "day" RegExp in the second format. What I'd do, is to take the two patterns highlighted, as they match the entire date format, and scrap the rest. Then I'd merge them together, and possibly move the binding caret out. Like this: /^(?:\d{2}\.\d{2}\.\d{4}|\d{4}\.\d{2}\.\d{2})\z/ That'll match either of the two date formats, and only that. If there's anything else in the string it'll fail. If you want to grab a certain subset of the date, then add a single pair of parenthesis around the section on both sides of the pipe. Note that doing it will cause you to get the result in two groups. Group #1 will be from the first format, and group #2 from the second. Only one of these will contain a value at any given time. Edited December 5, 2012 by Christian F. Quote Link to comment https://forums.phpfreaks.com/topic/271627-date-format/#findComment-1397699 Share on other sites More sharing options...
PFMaBiSmAd Posted December 5, 2012 Share Posted December 5, 2012 We have a field on the page Here's a slightly different slant on the problem. You should not allow visitors to type in entire dates, even if you display the expected format(s) right next to where they are entering the dates, because you will end up with some visitors that originally came from different locations or that don't/didn't read the expected format(s), entering dates with the months/days transposed. You should use three separate drop-down select menus and/or a javascript pop-up datepicker that would submit the year, month, and day in one known format. Quote Link to comment https://forums.phpfreaks.com/topic/271627-date-format/#findComment-1397700 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.