Giorgi Moniava
Giorgi Moniava

Reputation: 28654

Regex matching date pattern

I want to match filenames with date format and ANY extension. Is following regex good for this:

[0-9]{2}.[0-9]{2}.[0-9]{4}(?:\..*)?

??

Above regex, although seems to work, does not return this string in result: "12.1.1990.txt" How to write regex which also includes dates like either

12.12.2203.bmp
or
1.11.2005.txt
or
12.1.2006.bin
?

Thanks. And ps. where to find further info on writing these expressions?

Upvotes: 1

Views: 1943

Answers (3)

Lucaci Andrei
Lucaci Andrei

Reputation: 407

I'd go for this regex:

\d{1,2}.\d{1,2}.\d{4}(?:\.\w*)?

Also, I'm using some online regex tools for checking my regex, which is very handy on checking specific things (not to advertise or anything).

By this website, FileExt, DOS illegal characters in file extension are this: |<>\^=?/[]";* plus control characters, so changing the file extension regex to this one: (?:\.[^\|\<\>\\\^\=\?\/\[\]\"\;\*\.]*)? will match valid file extensions.

The final regex would be: \d{1,2}.\d{1,2}.\d{4}(?:\.[^\|\<\>\\\^\=\?\/\[\]\"\;\*\.]*)?

Regex101

Upvotes: 0

V2Solutions - MS Team
V2Solutions - MS Team

Reputation: 1127

Try this -

[0-9]{2}.[0-9]**{1,2}**.[0-9]{4}(?:\..*)?

Upvotes: 0

anubhava
anubhava

Reputation: 784968

You can use this regex:

^[0-9]{1,2}\.[0-9]{1,2}\.[0-9]{4}(?:\..*)?$

RegEx Demo

Upvotes: 2

Related Questions