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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T02:10:04+00:00 2026-05-11T02:10:04+00:00

I’m writing (in C#) a simple parser to process a scripting language that looks

  • 0

I’m writing (in C#) a simple parser to process a scripting language that looks a lot like classic C.

On one script file I have, the regular expression that I’m using to recognize /* block comments */ is going into some kind of infinite loop, taking 100% CPU for ages.

The Regex I’m using is this:

/\*([^*]|[\r\n]|(\*+([^*/]|[\r\n])))*\*+/ 

Any suggestions on why this might get locked up?

Alternatively, what’s another Regex I could use instead?

More information:

  • Working in C# 3.0 targeting .NET 3.5;
  • I’m using the Regex.Match(string,int) method to start matching at a particular index of the string;
  • I’ve left the program running for over an hour, but the match isn’t completed;
  • Options passed to the Regex constructor are RegexOptions.Multiline and RegexOptions.IgnorePatternWhitespace;
  • The regex works correctly for 452 of my 453 test files.
  • 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. 2026-05-11T02:10:04+00:00Added an answer on May 11, 2026 at 2:10 am

    Some problems I see with your regex:

    There’s no need for the |[\r\n] sequences in your regex; a negated character class like [^*] matches everything except *, including line separators. It’s only the . (dot) metacharacter that doesn’t match those.

    Once you’re inside the comment, the only character you have to look for is an asterisk; as long as you don’t see one of those, you can gobble up as many characters you want. That means it makes no sense to use [^*] when you can use [^*]+ instead. In fact, you might as well put that in an atomic group — (?>[^*]+) — because you’ll never have any reason to give up any of those not-asterisks once you’ve matched them.

    Filtering out extraneous junk, the final alternative inside your outermost parens is \*+[^*/], which means ‘one or more asterisks, followed by a character that isn’t an asterisk or a slash’. That will always match the asterisk at the end of the comment, and it will always have to give it up again because the next character is a slash. In fact, if there are twenty asterisks leading up to the final slash, that part of your regex will match them all, then it will give them all up, one by one. Then the final part — \*+/ — will match them for keeps.

    For maximum performance, I would use this regex:

    /\*(?>(?:(?>[^*]+)|\*(?!/))*)\*/ 

    This will match a well-formed comment very quickly, but more importantly, if it starts to match something that isn’t a valid comment, it will fail as quickly as possible.


    Courtesy of David, here’s a version that matches nested comments with any level of nesting:

    (?s)/\*(?>/\*(?<LEVEL>)|\*/(?<-LEVEL>)|(?!/\*|\*/).)+(?(LEVEL)(?!))\*/ 

    It uses .NET’s Balancing Groups, so it won’t work in any other flavor. For the sake of completeness, here’s another version (from RegexBuddy’s Library) that uses the Recursive Groups syntax supported by Perl, PCRE and Oniguruma/Onigmo:

    /\*(?>[^*/]+|\*[^/]|/[^*])*(?>(?R)(?>[^*/]+|\*[^/]|/[^*])*)*\*/ 
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 119k
  • Answers 119k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer You can detect if an object has a property without… May 11, 2026 at 11:53 pm
  • Editorial Team
    Editorial Team added an answer if you happen to be using the mysqli extension and… May 11, 2026 at 11:53 pm
  • Editorial Team
    Editorial Team added an answer You would write a module to manage the one-time URLs… May 11, 2026 at 11:53 pm

Related Questions

I ran into a problem. Wrote the following code snippet: teksti = teksti.Trim() teksti
I am currently running into a problem where an element is coming back from
Seemingly simple, but I cannot find anything relevant on the web. What is the
Does anyone know how can I replace this 2 symbol below from the string
Configuring TinyMCE to allow for tags, based on a customer requirement. My config is

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.