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

  • Home
  • SEARCH
  • 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 7879519
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T03:54:44+00:00 2026-06-03T03:54:44+00:00

As explained in Can regular expressions be used to match nested patterns? , it

  • 0

As explained in Can regular expressions be used to match nested patterns?, it is not possible to create regex to match arbitrary nested pattern. But is it possible to create an algorithm that would generate a regex of n-th level of “nesteness”?

basically, i want to replace trim(whatever) with rtrim(ltrim(whatever))

i managed to create 3 levels by hand (javascript syntax):

level[1] = /\(([^()]*)\)/g
level[2] = /\(((?:[^()]*\([^()]*\))*[^()]*)\)/g
level[3] = /\(((?:(?:(?:[^()]*\([^()]*\))*[^()]*)*\((?:(?:[^()]*\([^()]*\))*[^()]*)*\))*[^()]*)\)/g

here are some test data:

1st(ddd) + 1st(ddd)
2nd(dd(d))
3rd(a(b) + (cd(h) + d(dfas) + zzz))
4th(a(b(c(d))))
8th(a(b(c(d(e(f(g()))))))

i know that at every level [^()]* needs to be replaced with noncapturing group that can contain parentheses, but i’m not sure how to generalize the algoritm for n-th level…

  • 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-06-03T03:54:45+00:00Added an answer on June 3, 2026 at 3:54 am

    You can think about it more theoretically: a match for parenthesis nested n deep is just parenthesis around matches for n-1 or less deep (with at least one exactly n-1 deep).

    We can give a recursive definition of the regexes. Let X[n] be the regex for nesting exactly n levels, and Y[n] be the regex for a string containing brackets with any level of nesting up to n levels, so:

    X[n] = \( (Y[n-2] X[n-1])+ Y[n-2] \)
    
    Y[n] = [^()]* ( \( Y[n-1] \) [^()]* )*
    

    with Y[0] = X[0] = [^()]* (no nesting) and X[1] = \([^()]*\). (I’m not bothering with the details of non-capturing groups etc yet, and the spaces are just for readability.)

    Writing an algorithm based on this should be quite easy.


    The regexes from these new (less mutually recursive) definitions get longer much much more slowly (they are polynomial rather than exponential).

    Let l[n] be the length of X[n], and L[n] be the length of Y[n], then (the constant terms are just the hardcoded characters in each one):

    L[n] = 19 + L[n-1] = 19*n + L[0] = 19*n + 6
    
    l[n] = 3 + L[n-2] + l[n-1] + 2 + L[n-2] + 2
         = 7 + 2 * L[n-2] + l[n-1]
         = -57 + 38 * n + l[n-1]
    

    with the appropriate initial conditions for l[0] and l[1]. Recurrence relations of this form have quadratic solutions, so this is only O(n^2). Much better.

    (For others, I had a previous definition of Y[n] was Y[n] = Y[n-1] | X[n]; this extra recursion meant that the length of the X regex was O(2.41^n), which sucks a lot.)

    (The new definition of Y is a tantalising hint that there might even be a way of writing X that is linear in n. I don’t know though, and I have a feeling the extra restriction on X of exact length means it is impossible.)


    The following is some Python code that computes the regexes above, you can probably translate it to javascript without too much trouble.

    # abbreviation for the No Parenthesis regex
    np = "[^()]*"
    
    # compute Y[n] from Y[n-1]
    def next_y(y_n1):
        return np + "(?:\(" + y_n1 + "\)" + np + ")*"
    
    # compute X[n] from X[n-1] and Y[n-2]
    def next_x(x_n1, y_n2):
        return "\((?:" + y_n2 + x_n1 + ")+" + y_n2 + "\)"
    
    # compute [X[n], Y[n], Y[n-1]]
    # (to allow us to make just one recursive call at each step)
    def XY(n):
        if n == 0:
            return [np, # X[0]
                    np, # Y[0]
                    ""] # unused
        elif n == 1:
            return ["\([^()]*\)", # X[1]
                    next_y(np),   # Y[1]
                    np]           # Y[0]
    
        x_n1, y_n1, y_n2 = XY(n-1) # X[n-1], Y[n-1], Y[n-2]
    
        return [next_x(x_n1, y_n2), # X[n]
                next_y(y_n1),       # Y[n]
                y_n1]               # Y[n-1]
    
    # wrapper around XY to compute just X[n]
    def X(n):
        return XY(n)[0]
    
    # wrapper around XY to compute just Y[n]
    def Y(n):
        return XY(n)[1]
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

src.match(/^(https?\:\/\/.*)\//) I know regular expressions but the syntax is not familiar to me. Can
In What regular expressions can never match? /$./ was given as a response. I
Can someone explain this to me. I am pretty good with perl regular expressions
We're learning about the difference between regular languages and regex, and the teacher explained
Friends, I'm new to both Javascript and Regular Expressions and hope you can help!
I'm just wondering if it's possible to use one regular expression to match another,
Besides regular expressions and selectors, where can I learn what the symbols mean.. such
I am learning regular expressions and I am trying to create one that will
I don’t understand or see the need for regular expressions. Can some explain them
Maybe I'm not understanding something, but can somebody explain to me why the following

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.