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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T18:09:07+00:00 2026-06-02T18:09:07+00:00

Assuming I have a string such as: abc(def(gh)il(mn(01))afg)lmno(sdfg*) How can I determine the matching

  • 0

Assuming I have a string such as:

abc(def(gh)il(mn(01))afg)lmno(sdfg*)

How can I determine the matching bracket for the first one? (meaning (def(gh)il(mn(01))afg))

I have tried to create a between function by counting the number of open brackets until the first ‘)’, but it doesn’t work on strings like this one.

  • 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-02T18:09:10+00:00Added an answer on June 2, 2026 at 6:09 pm

    You could use a function that simply traverses the string while keeping track of a stack of indices of opening parentheses. Whenever you encounter a closing parenthesis, you know that it matches with the index at the top of the stack.

    For example:

    parenPairs :: String -> [(Int, Int)]
    parenPairs = go 0 []
      where
        go _ _        []         = []
        go j acc      ('(' : cs) =          go (j + 1) (j : acc) cs
        go j []       (')' : cs) =          go (j + 1) []        cs -- unbalanced parentheses!
        go j (i : is) (')' : cs) = (i, j) : go (j + 1) is        cs
        go j acc      (c   : cs) =          go (j + 1) acc       cs
    

    This function returns a list of all pairs of indices belonging to pairs of matching parentheses.

    Applying the function to your example string gives:

    > parenPairs "abc(def(gh)il(mn(01))afg)lmno(sdfg*)"
    [(7,10),(16,19),(13,20),(3,24),(29,35)]
    

    The opening parenthesis you were interested in appears at index 3. The returned list shows that the matching closing parenthesis is to be found at index 24.

    The following functions gives you all properly parenthesised segments of a string:

    parenSegs :: String -> [String]
    parenSegs s = map (f s) (parenPairs s)
      where
        f s (i, j) = take (j - i + 1) (drop i s)
    

    For example:

    > parenSegs "abc(def(gh)il(mn(01))afg)lmno(sdfg*)"
    ["(gh)","(01)","(mn(01))","(def(gh)il(mn(01))afg)","(sdfg*)"]
    

    Following Frerich Raabe’s suggestion, we can now also write a function that only returns the leftmost segment:

    firstParenSeg :: String -> String
    firstParenSeg s = f s (minimum (parenPairs s))
      where
        f s (i, j) = take (j - i + 1) (drop i s)
    

    For example:

    > firstParenSeg "abc(def(gh)il(mn(01))afg)lmno(sdfg*)"
    "(def(gh)il(mn(01))afg)"
    

    Note that firstParenSeg will fail if the input string does not contain at least one pair of matching parentheses.

    Finally, a small adaption of the parenPairs function lets it fail on unbalanced parentheses:

    parenPairs' :: String -> [(Int, Int)]
    parenPairs' = go 0 []
      where
        go _ []        []         = []
        go _ (_ : _ )  []         = error "unbalanced parentheses!"
        go j acc       ('(' : cs) =          go (j + 1) (j : acc) cs
        go j []        (')' : cs) = error "unbalanced parentheses!"
        go j (i : is)  (')' : cs) = (i, j) : go (j + 1) is        cs
        go j acc       (c   : cs) =          go (j + 1) acc       cs
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Assuming I have an event subscription such as _vm.PropertyChanged += OnViewModelAmountChanged; How can I
Assuming i have a helper such as: @helper AddErrorSpan(string error) { <span class=error>@error</span> }
Assuming I have the following strings: string str1 = Hello World!; string str2 =
Assuming the message argument is a string, I have the following snippet: users.each do
Assuming I have the following two JQuery functions - The first, which works: $(#myLink_931).click(function
Assuming I have a comments model and a posts model, What code can I
I have 2 <h:form> elements following each other (not one within the other) such
Having val a: IndexedSeq[String] = Array(one, two, three) def f(s: String): Int = s
I have a HashMap as below (assuming it has 10,0000 elements) HashMap<String,String> hm =
Assuming we have such control: public partial class MyUserControl : UserControl { public MyUserControl()

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.