Kartheek Palepu
Kartheek Palepu

Reputation: 972

Need a modification in the regular expression

I have regular expressions

'\d{2}[-/.]\d{2}[-/.]\d{4}' # For dd-mm-yyyy or dd/mm/yyyy or dd.mm.yyyy.

But, it is also pulling out dd-mm.yyyy or dd/mm-yyyy from the string. I need a strict match for the dates i.e. dd-mm-yyyy or dd/mm/yyyy or dd.mm.yyyy.

Upvotes: 2

Views: 53

Answers (1)

Sufian Latif
Sufian Latif

Reputation: 13356

You can use this:

\d{2}([-/.])(\d{2}|[a-zA-Z]{3})\1\d{4}

([-/.]) will match -, / or . as a group, and \1 anywhere in this regex will refer to the same text matched by the group.

See demo here.

Upvotes: 2

Related Questions