Regular Expressions

Can a date in various format be extracted from SMS received using regex?

Most probably yes
Do you have some example sms texts?
Regex specialists you can find at

Taifun

Some date formats :
02 February 2026
1st September 2025
18/08/2025

Here is an answer from ChatGPT
Taifun
.---

You can handle all three formats with one regex, as long as you allow for:

  • Day: 1 or 2 digits, sometimes with st/nd/rd/th
  • Month: Full month name or numeric month
  • Year: Always 4 digits

Recommended Regex (most flexible)

\b(?:(\d{1,2})(?:st|nd|rd|th)?(?:[\/\-\.\s](\d{1,2})[\/\-\.\s](\d{4})|(?:\s+(January|February|March|April|May|June|July|August|September|October|November|December)\s+(\d{4})))\b

This will match:

Example Matches? Explanation
02 February 2026 :white_check_mark: Day + month name + year
1st September 2025 :white_check_mark: Day + ordinal + month name + year
18/08/2025 :white_check_mark: Numeric date format

If you want a simpler "just capture the whole date as text" regex:

\b\d{1,2}(?:st|nd|rd|th)?\s+(January|February|March|April|May|June|July|August|September|October|November|December)\s+\d{4}\b|\b\d{1,2}[\/\-\.]\d{1,2}[\/\-\.]\d{4}\b

This matches all your examples and is easier to mentally parse.

How about 1/2/2025?

Is it January 2, 2025 or is it February 1,2025?

How would a regex know?

Thanks Taifun.
Will explore based on your reply.