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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T07:09:29+00:00 2026-05-31T07:09:29+00:00

The code below contains a regular expression designed to extract a C# string literal

  • 0

The code below contains a regular expression designed to extract a C# string literal but the performance of the regex matching for input strings of more than a few characters is woeful.

class Program
{
   private static void StringMatch(string s)
    {
        // regex: quote, zero-or-more-(zero-or-more-non-backslash-quote, optional-backslash-anychar), quote
        Match m = Regex.Match(s, "\"(([^\\\\\"]*)(\\\\.)?)*\"");
        if (m.Success)
            Trace.WriteLine(m.Value);
        else
            Trace.WriteLine("no match");
    }

    public static void Main()
    {
        // this first string is unterminated (so the match fails), but it returns instantly
        StringMatch("\"OK");

        // this string is terminated (the match succeeds)
        StringMatch("\"This is a longer terminated string - it matches and returns instantly\"");

        // this string is unterminated (so the match will fail), but it never returns
        StringMatch("\"This is another unterminated string and takes FOREVER to match");
    }
}

I can refactor the regex into a different form, but can anyone offer an explanation why the performance is so bad?

  • 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-31T07:09:30+00:00Added an answer on May 31, 2026 at 7:09 am

    You’re running into catastrophic backtracking:

    Let’s simplify the regex a bit (without the escaped quotes and without the second optional group because, as in your comment, it’s irrelevant for the tested strings):

    "(([^\\"]*))*" 
    

    ([^\\"]*) matches any string except quotes or backslashes. This again is enclosed in an optional group that can repeat any number of times.

    Now for the string "ABC, the regex engine needs to try the following permutations:

    • ", ABC
    • ", ABC, <empty string>
    • ", AB, C
    • ", AB, C, <empty string>
    • ", AB, <empty string>, C
    • ", AB, <empty string>, C, <empty string>
    • ", <empty string>, AB, C
    • ", <empty string>, AB, C, <empty string>
    • ", <empty string>, AB, <empty string>, C, <empty string>
    • ", <empty string>, AB, <empty string>, C
    • ", A, BC
    • ", A, BC, <empty string>
    • ", A, <empty string>, BC
    • ", <empty string>, A, BC
    • etc.
    • ", A, B, C
    • ", A, B, C, <empty string>
    • ", A, B, <empty string>, C
    • etc. etc.

    each of which then fails because there is no following ".

    Also, you’re only testing for substrings instead of forcing the regex to match the entire string. And you usually want to use verbatim strings for regexes to cut down on the number of backslashes you need. How about this:

    foundMatch = Regex.IsMatch(subjectString, 
        @"\A     # Start of the string
        ""       # Match a quote
        (?:      # Either match...
         \\.     # an escaped character
        |        # or
         [^\\""] # any character except backslash or quote
        )*       # any number of times
        ""       # Match a quote
        \Z       # End of the string", 
        RegexOptions.IgnorePatternWhitespace);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Code below contains a JS function that receives as parameter String €€ (Euro sign).
Please go through the code below. String[] Info contains 2 values like this: shiftDirection
Hello friends i am running code given below which contains the setLogTimeEntery function and
The code below works great. I have a MySQL database that contains book titles
I have the below code for my a Dialog box for a which contains
Below is my entire code from a User control that contains the YUI Uploader.
I'm having some issues with a regular expression I'm creating. I need a regex
I have an array (called array in the code below) which contains a number
Consider the code below: #if DEBUG if (Systems.Contains(system)) throw new InvalidOperationException(System already registered); #endif
A DialogViewController contains an EntryElement and RefreshRequested handler is set (see code below). Run

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.