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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T09:22:12+00:00 2026-05-26T09:22:12+00:00

I was actually optimizing Regex transformations of large strings. As some calls to Regex.Replace

  • 0

I was actually optimizing Regex transformations of large strings. As some calls to Regex.Replace took significant time I inserted conditional calls – something along the lines

if (str.IndexOf("abc", 0, StringComparison.CurrentCultureIgnoreCase) > 0)
    str1 = Regex.Replace(str, "abc...", string.Empty, RegexOptions.IgnoreCase);

To my surprise the results were not convincing. Sometimes better, sometimes not. Or even worse. So I measured performance of case insensitive IndexOf (I tried also StringComparison.OrdinalIgnoreCase) and found that it may be slower than Regex.Match, i.e. this test:

if( Regex.Match(str,"abc", RegexOptions.IgnoreCase).Success )...

Particularly for a large string that did not match (ascii) string “abc” I found that Regex.Match was 4x faster.

Even this procedure is faster than IndexOf:

string s1 = str.ToLower();
if( s1.IndexOf("abc") ) ...

Anybody knows a better solution?

  • 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-26T09:22:13+00:00Added an answer on May 26, 2026 at 9:22 am

    Because indexOf is O(n*m) where RegEx will likley be O(n+m) (where n= string length, m= search string length).

    If you are serious ablut searching substrings it would be useful to read about string search algorithms to be at least aware of expected speeds (start with http://en.wikipedia.org/wiki/Substring_search ).

    Note: Culture sensetive comparison may be significantly slower than Ordinal (depending on your scenario you may not be able to use Ordinal versions).

    As with any performance questions one need to get out and measure. In indexOf with Regex.isMatch clear winner for me is Regex. I did expected the behavior as indexOf should not perform any pre-compile of the search string and have to use O(n+m) algorithm, while Regex have to use much more optimal implemetation.

    Try to measure following searches – I get almost 5x difference in favor of Regex for 100K operations.

       static void Main(string[] args)
        {
            var stringToSearch = "AAAAAAAAAAAAAAAAAAAAbAAAAAAAAAAAAAAAAAAAAAbb";
            Regex regExp = new Regex(stringToSearch, RegexOptions.Compiled | RegexOptions.CultureInvariant | RegexOptions.IgnoreCase);
    
            var sourceText = "AAAAAAAAAAAAAAAAAAAAbAAAAAAAAAAAAAAAAAAAAAbAAAAAAAAAAAAAAAAAAAAbAAAAAAAAAAAAAAAAAAAAAbAAAAAAAAAAAAAAAAAAAAbAAAAAAAAAAAAAAAAAAAAAbAAAAAAAAAAAAAAAAAAAAbAAAAAAAAAAAAAAAAAAAAAbb";
    
           const int Iterations = 100000;
    
           Stopwatch watch = new Stopwatch();
           watch.Start();
           for (int i = 0; i < Iterations; i++)
           {
               regExp.IsMatch(sourceText);
           }
           watch.Stop();
           Console.WriteLine("RegExp: {0} ms", watch.ElapsedMilliseconds);
    
           watch = new Stopwatch();
           watch.Start();
           for (int i = 0; i < Iterations; i++)
           {
               sourceText.IndexOf(stringToSearch, StringComparison.OrdinalIgnoreCase);
           }
           watch.Stop();
           Console.WriteLine("RegExp: {0} ms", watch.ElapsedMilliseconds);
           Console.ReadLine();
        }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Actually I am trying to move some box alternatively with in another box. I
Actually I'm always using Generic Collections and I frequently use List<>. Some scenarios I
Actually, this question seems to have two parts: How to implement pattern matching? How
Actually, I'm using this way. Do you have a better way? private bool AcceptJson(HttpRequest
Actually here is a similar topic with little practical value. As far as I
Actually my question is all in the title. Anyway: I have a class and
Actually what i am trying to build is like a kind of firewall. It
Actually in the center of current view, above all other objects. Cross-browser and without
Actually I have two questions. (1) Is there any reduction in processing power or
Actually I can save it in swf and in avi formats. Is there any

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.