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

The Archive Base Latest Questions

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

I am trying to create a .NET RegEx expression that will properly balance out

  • 0

I am trying to create a .NET RegEx expression that will properly balance out my parenthesis. I have the following RegEx expression:

func([a-zA-Z_][a-zA-Z0-9_]*)\(.*\)

The string I am trying to match is this:

"test -> funcPow((3),2) * (9+1)"

What should happen is Regex should match everything from funcPow until the second closing parenthesis. It should stop after the second closing parenthesis. Instead, it is matching all the way to the very last closing parenthesis. RegEx is returning this:

"funcPow((3),2) * (9+1)"

It should return this:

"funcPow((3),2)"

Any help on this would be appreciated.

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

    Regular Expressions can definitely do balanced parentheses matching. It can be tricky, and requires a couple of the more advanced Regex features, but it’s not too hard.

    Example:

    var r = new Regex(@"
        func([a-zA-Z_][a-zA-Z0-9_]*) # The func name
    
        \(                      # First '('
            (?:                 
            [^()]               # Match all non-braces
            |
            (?<open> \( )       # Match '(', and capture into 'open'
            |
            (?<-open> \) )      # Match ')', and delete the 'open' capture
            )+
            (?(open)(?!))       # Fails if 'open' stack isn't empty!
    
        \)                      # Last ')'
    ", RegexOptions.IgnorePatternWhitespace);
    

    Balanced matching groups have a couple of features, but for this example, we’re only using the capture deleting feature. The line (?<-open> \) ) will match a ) and delete the previous “open” capture.

    The trickiest line is (?(open)(?!)), so let me explain it. (?(open) is a conditional expression that only matches if there is an “open” capture. (?!) is a negative expression that always fails. Therefore, (?(open)(?!)) says “if there is an open capture, then fail”.

    Microsoft’s documentation was pretty helpful too.

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

Sidebar

Related Questions

We're trying to create a .NET aspx page that will have a PDF within
I am trying to create a .Net Regex that will separate an Excel header
I am trying to create a regex that will take all files that do
I am trying to create an asp.net web form that allows a user to
I'm trying to create a Zip file from .Net that can be read from
im trying to create a database for a feedback application in ASP.net i have
I am trying to create a .NET Regex to parse a CSS font declaration,
I am trying to create a regex validation attribute in asp.net mvc to validate
I'm trying to create an ASP.NET/C# page that allows the user to edit a
As of right now I am trying to create an ASP.NET page which will

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.