Of course! To match 2025-06-09 02:38 PM, you need to make three main changes to your original regex.
Your original regex was for a 24-hour format with seconds:
[0-9]{4}-(0[1-9]|1[0-2])-(0[1-9]|[1-2][0-9]|3[0-1]) (2[0-3]|[01][0-9]):[0-5][0-9]:[0-5][0-9]
Your target text uses a 12-hour format, has no seconds, and includes an AM/PM indicator.
Here is the modified regex:
[0-9]{4}-(0[1-9]|1[0-2])-(0[1-9]|[1-2][0-9]|3[0-1]) (0[1-9]|1[0-2]):[0-5][0-9] (AM|PM)
Breakdown of the Changes
-
Hour Format: The original regex had (2[0-3]|[01][0-9]) which is for a 24-hour clock (00-23). Your text uses a 12-hour clock (01-12).
- Changed to:
(0[1-9]|1[0-2]) which matches numbers from 01 to 12.
-
Removed Seconds: Your text 02:38 PM does not have seconds, so the part that matches a colon and two digits for seconds was removed.
-
Added AM/PM: Your text ends with PM. A new part was added to match a space followed by either "AM" or "PM".
Final Regex Explained
Here is a piece-by-piece explanation of the new regex:
[0-9]{4}: Matches exactly four digits for the year (e.g., 2025).
-: Matches the literal hyphen.
(0[1-9]|1[0-2]): Matches the month (01-12).
-: Matches the literal hyphen.
(0[1-9]|[1-2][0-9]|3[0-1]): Matches the day (01-31).
: Matches the space character.
(0[1-9]|1[0-2]): Matches the 12-hour format hour (01-12).
:: Matches the literal colon.
[0-5][0-9]: Matches the minute (00-59).
: Matches the space character.
(AM|PM): Matches either "AM" or "PM".
A Simpler, More General Version
If you don't need to be extremely strict about validating the date (e.g., you trust the source of the text), you could use a much simpler version with \d which stands for any digit:
\d{4}-\d{2}-\d{2} \d{2}:\d{2} (AM|PM)
This is easier to read but would also incorrectly match an invalid date like 9999-99-99 99:99 PM. For your specific purpose of matching the text provided, both regexes will work perfectly.