Windows batch: Validating a date with regex using findstr /r

51 views Asked by At

I'm stuck on understanding why a similar regular expression is failing in a Windows batch file, or on the command line.

This regular expression works fine:

echo %Date%|findstr /r "^[mtwtfsMTWTFS][ouehra][nedurt] (3[01]|[12][0-9]|0?[1-9])(\/|-)(1[0-2]|0?[1-9])\2([0-9]{2})?[0-9]{2}$"

As expected, it returns:

Mon 03/11/2024

This code fails:

set passed_date=03/11/2024
echo %passed_date%|findstr /r "^(3[01]|[12][0-9]|0?[1-9])(\/|-)(1[0-2]|0?[1-9])\2([0-9]{2})?[0-9]{2}$"

It doesn't return anything, so I'm unclear as to why the second regular expression is failing.

I rewrote the regex:

echo %passed_date%|findstr /r "^(0[1-9]|1[0-2])(\/)(0[1-9]|[1-2][0-9]|3[0-1])(\/)(2[0-9][0-9]{2})$"

But it still doesn't return anything.

I also placed the regex into REGEX 101, where it extracts what I was looking for.

I'm not interested in using any alternative methods of validating the date, I'm just trying to understand why one works and one doesn't.

0

There are 0 answers