Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Java Regular Expressions in Java

Tahir Imran
Tahir Imran
9,346 Points

Universal date regex

Hi i have a date of a format d/mm/yyyy example(1/11/2016), so the date format may vary depending on the date example if the date is 25/7/2016 then the format is dd/m/yyyy could you please let me know what can be the best way to achieve this regex. I tried the one which someone else had mentioned in the questions related questions but that didnt work. Thanks.

well a simple regex would be something like "^\d{1}-\d{2}-\d{4}" and you could modify it to the couple cases you wish to receive like "^\d{2}-\d{1}-\d{4}" and use a or (||) logical expression

1 Answer

Tahir, I suggest you go to the Regex Library (http://www.regxlib.com/Search.aspx?k=date). At present there are 754 regex expressions for dates. I'm guessing someone else has already written one that will accept the dates you need: those with days and months with either 1 or 2 digits each. The description for each expression listed almost always gives examples of the dates that will pass and those that will fail.