Regex for date formats

Hi,
I am looking for regex for which accepts only below formats.
MM/DD/YY
MM/DD/YYYY
MM-DD-YY
MM-DD-YYYY

1 Like

Hi @SN2

Are you searching the same Regex which i have mention below
image

Mark as solution and like it if this helps you :slight_smile:

Happy Automation :raised_hands:

Best Regards
Er Pratik Wavhal :robot::man_technologist:t4: :computer:

2 Likes

It’s not working for me.
image
please suggest me.

Hi @SN2

In the previous post i have just given the format as you provided. I thought you will put for digit as of you haven’t given any data to test so.

Below is the updated Regex with dummy data
image

Mark as solution and like it if this helps you :slight_smile:

Happy Automation :raised_hands:

Best Regards
Er Pratik Wavhal :robot::man_technologist:t4: :computer:

2 Likes

Hi @SN2,

You can use the following RegEx.

^\d{1,2}/\d{1,2}/\d{2,4}$

Warm Regards,
Ranjith Udayakumar

Hi

Hope this would help you in addition to above suggestions

(\d){1,2}\W(\d){1,2}\W(\d){2,4}

here
\d - stands for digits 0-9
{1,2} - stands for length from 1 to 2 digits
\W - stands for any non word character which can be either \ or -
{2,4} - stands for length from 2 to 4 digits,

same apply for months and years…that’s it

Cheers @SN2

1 Like

@SN2

Hi,

You can use the following pattern that at least ensure the separator is consistent and that the date is a cohesive group:

"\b\d{2}([-/])\d{2}\1(\d{2}|\d{4})\b"

If you really need to ensure that the numbers for month and day are in their respective range, you might use something more elaborate that expands the constrains were month is from 01 to 12 and day from 01 to 31 (without any further check), like:

"\b(?<month>0[1-9]|1[0-2])(?<sep>[-/])(?<day>0[1-9]|[1-2]\d|3[0-1])\k<sep>(?<year>\d{2}|\d{4})\b"

If you’re looking for something more accurate, you might validate matches against a function that will return True if the string is valid against any chosen datetimeFormat with DateTime.ParseExact method.

1 Like

Hi @Pratik_Wavhal,

Thanks for your solution. :grinning:

Thanks, @ranjith_udayakumar, @Palaniyappan, and @msan for solution and explanation.

2 Likes

This topic was automatically closed 3 days after the last reply. New replies are no longer allowed.