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 7700165
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T22:38:44+00:00 2026-05-31T22:38:44+00:00

I’m looking for a fast way in C# to find all dates in a

  • 0

I’m looking for a fast way in C# to find all dates in a string (the string is a big text, I’ve to scan for about 200,000 different strings).

since there are many ways to write date (for example 31/12/2012 or Dec 31, 2012 and much more),
I’m using this Regex (that should cover almost all frequent ways to write dates):

string findDates = “(?:(\d{1,4})- /.-
/.)|(?:(\s\d{1,2})\s+(jan(?:uary){0,1}\.{0,1}|feb(?:ruary){0,1}\.{0,1}|mar(?:ch){0,1}\.{0,1}|apr(?:il){0,1}\.{0,1}|may\.{0,1}|jun(?:e){0,1}\.{0,1}|jul(?:y){0,1}\.{0,1}|aug(?:ust){0,1}\.{0,1}|sep(?:tember){0,1}\.{0,1}|oct(?:ober){0,1}\.{0,1}|nov(?:ember){0,1}\.{0,1}|dec(?:ember){0,1}\.{0,1})\s+(\d{2,4}))|(?:(jan(?:uary){0,1}\.{0,1}|feb(?:ruary){0,1}\.{0,1}|mar(?:ch){0,1}\.{0,1}|apr(?:il){0,1}\.{0,1}|may\.{0,1}|jun(?:e){0,1}\.{0,1}|jul(?:y){0,1}\.{0,1}|aug(?:ust){0,1}\.{0,1}|sep(?:tember){0,1}\.{0,1}|oct(?:ober){0,1}\.{0,1}|nov(?:ember){0,1}\.{0,1}|dec(?:ember){0,1}\.{0,1})\s+([0-9]{1,2})[\s,]+(\d{2,4}))”;

with “RegexOptions.Compiled | RegexOptions.IgnoreCase | RegexOptions.IgnorePatternWhitespace” tags.
also, i tried to pre-compile the regex to make it even more fast.

The problem is that it’s very slow (on some text fies more than 2 seconds)
Is there any better and efficient way to do this?

Thanks

  • 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-31T22:38:45+00:00Added an answer on May 31, 2026 at 10:38 pm

    The expression looks good overall, as others have mentioned, it might be a bit verbose with all the {0,1} instead of ? and the (?: instead of applying RegexOptions.ExplicitCapture. But these shouldn’t make the expression slow. They only result in better readability.

    What might be causing slowness is the fact that there’s a lot of backtracking options in the expression by making both the expanded month and the . optional. I wonder what would happen if you changed the expression to only apply the optional . once, after the month name, and what would happen if you made the month name a greedy group ((?>pattern) Nonbacktracking (or “greedy”) subexpression.)

    So that:

     (jan(?:uary){0,1}\.{0,1}|feb(?:ruary){0,1}\.{0,1}|mar(?:ch){0,1}\.{0,1}|apr(?:il){0,1}\.{0,1}|may\.{0,1}|jun(?:e){0,1}\.{0,1}|jul(?:y){0,1}\.{0,1}|aug(?:ust){0,1}\.{0,1}|sep(?:tember){0,1}\.{0,1}|oct(?:ober){0,1}\.{0,1}|nov(?:ember){0,1}\.{0,1}|dec(?:ember){0,1}\.{0,1})\s+(\d{2,4}))
    

    Would become:

     (?>jan(uary)?|feb(ruary)?|mar(ch)?|apr(il)?|may|june?|july?|aug(ust)?|sep(tember)?|oct(ober)?|nov(ember)?|dec(ember)?)\.?\s+(\d{2,4}))
    

    Not only is it much shorter, I’d expect it to be faster.

    And then there’s the part of the expression at the start, which doesn’t really make sense to me (?:(\d{1,4})- /.- /.) Either something got lost in the formatting, or this isn’t helping one bit.

    \d{1,4} would make sense for a year or any other date part, but the - /.- /. after it doesn’t make sense at all. I think you meant something like:

     \d{1,4}[- /.]\d{1,2}[- /.]\d{1,2}
    

    Or something in that area. As it stands it captures garbage, probably not speeding up the matching process.

    In the end I agree with Aliostad, that you’re probably better off trying to find a less precise pattern to find initial candidates, then narrow the results using either DateTime.TryParseExact or with an additional set of expressions.

    Instead of creating a ‘global’ expression to find candidates, you could use a lot of exact expressions. You’ll see that with Regex it’s often cheaper to run a number of exact expressions over a large input, than running one expression with a lot of |’s and ?’s in there.

    So breaking down your search into multiple very exact expressions might result in a lot higher performance, these could be a start:

     \b\d{1,2}[- .\\/]\d{1,2}[- .\\/](\d{2}|\d{4})\b
     \b((jan|feb|mar|apr|jun|jul|aug|sep|oct|nov|dec)(.|[a-z]{0,10})|\d{1,2})[- .\\/,]\d{1,2}[- .\\/,](\d{2}|\d{4})\b
    

    As you can see all optional groups have been removed from these expressions, making them a lot faster to run. I’ve also removed the exact spelling from the month names, as you probably want to accept ‘sept’ as well as ‘sep’ as well as ‘september’

    Breaking up the pattern also improves readability :).

    One last tip: limit the amount of possible characters you need to backtrack, by putting a limit on things like \s+, you rarely want 20,000 spaces to match, but if they’re in your source document, it will try to match them. \s{1,20} is usually enough and limits the engines ability to try to get a match where there really isn’t one.

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

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
For some reason, after submitting a string like this Jack’s Spindle from a text
I have a text area in my form which accepts all possible characters from
I have a jquery bug and I've been looking for hours now, I can't
I would like to count the length of a string with PHP. The string
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I want to count how many characters a certain string has in PHP, but
I've got a string that has curly quotes in it. I'd like to replace
Specifically, suppose I start with the string string =hello \'i am \' me And
I am reading a book about Javascript and jQuery and using one of 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.