Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 344451
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T10:59:31+00:00 2026-05-12T10:59:31+00:00

I have this Regular Expression that matches the following strings: <!– 09-02-2009 —> <!–

  • 0

I have this Regular Expression that matches the following strings:

<!-- 09-02-2009 --->
<!-- 09-02-2009 12:00:00 --->
<!-- 09-02-2009 12:00:00 A --->
<!-- 09-02-2009 12:00:00 AM --->

Here is the pattern:

<!-- (?<month>\d{2}?)-(?<day>\d{2}?)-(?<year>\d{4}?)(?:(?: ?\d{2}:?){3}?(?: ?[aApP][mM]?)?)? --->

updated pattern, per twistol:

<!-- (?<month>\d{2}?)-(?<day>\d{2}?)-(?<year>\d{4}?)(?<time>(?: ?(?:\d{2}:){2}\d{2})?(?: ?[aApP][mM]?)?)? --->

Is there anything I can do to simplify this pattern?

Thanks!

EDIT

Here is the pattern I came up with all comments/answers, plus validation built in. It is a bit ugly, but who said regex needs to be pretty? 😛

<!-- (?<month>(?:0[1-9]|1[0-2]))-(?<day>(?:0[1-9]|1[0-9]|2[0-9]|3[01]))-(?<year>\d{4})(?<time> (?:0[0-9]|1[0-9]|2[0-3]):(?:[0-5][0-9])(?::[0-5][0-9])?(?: [aApP][mM]?)?)? --->

It will match valid dates in the following formats:

<!-- 09-02-2009 --->
<!-- 09-02-2009 12:00 --->
<!-- 09-02-2009 12:00 A --->
<!-- 09-02-2009 12:00 AM --->
<!-- 09-02-2009 12:00:00 --->
<!-- 09-02-2009 12:00:00 A --->
<!-- 09-02-2009 12:00:00 AM --->
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-12T10:59:31+00:00Added an answer on May 12, 2026 at 10:59 am
    <!-- (?<month>\d\d)-(?<day>\d\d)-(?<year>\d{4})(?: \d\d:\d\d:\d\d(?: [aApP][mM]?)?)? -->
    

    Is as simple as I can think of. Note that this regex isn’t exactly the same, since in the original the timestamp colons were all optional, meaning it would match 01:0203 or 0102:03:, etc. I think my version may be more correct.

    Basically I removed all the noncapturing groups and quantifiers I could, which when they are merely doubling a digit make it less readable, as opposed to more. I also removed the greediness modifier on the quantifiers, since they will always match exactly 2 or 4 or whatever whether it’s greedy or not.

    And of course, this will match invalid dates, such as 13-32-0000. To fix that, you will have to decide whether a complex yet correct solution is more desirable than a simple, more understandable one. Basically, it depends on your confidence in the text you will be running this over. If there are likely to be false positives that you want to filter out, go for a more correct solution, even if it is slightly less readable.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need some regular expression for .net that matches the following pattern : YYYYMM
I've got a regular expression that looks something like this: a(|bc) this expression matches
I have to write a regular expression that will match the following patterns, it
I have this regular expression: ^\$?(\d{1,3}(\,\d{3})*|(\d+))(\.\d{2})?$ however it is failing when i have an
I have a regular expression like this (much simplified): ^(ab)*$ And am matching against
Suppose I have the following two strings containing regular expressions. How do I coalesce
I'm trying to construct a regular expression that would match a pattern as such:
I have created the following Regular Expression in C# to extract tokens from a
I have this code in jQuery, that I want to reimplement with the prototype
I have this RewriteRule that works too well :-) RewriteRule ^([^/]*)/$ /script.html?id=$1 [L] The

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.