+ Reply to Thread
Results 1 to 17 of 17

How to make sure if every opening bracket has a closing one (with RegEx)

  1. #1
    Valued Forum Contributor ImranBhatti's Avatar
    Join Date
    03-27-2014
    Location
    Rawalpindi,Pakistan
    MS-Off Ver
    Office 365
    Posts
    1,785

    How to make sure if every opening bracket has a closing one (with RegEx)

    Hi

    I need to make sure that every opening bracket in a long string has also been closed at the end or vice versa.
    i.e
    string="abc(xyz)...gsdfs(asdfasdfadfaf
    this should put another brakcer at the end in order to make its pair.
    There are code snippets with a loop but I want to achieve it with RegEx.is it possible
    Reason I don't want to use loop ,is because I will have to loop over cells in a range and if every cell has a sub loop then the code will go slow.
    Teach me Excel VBA

  2. #2
    Forum Expert nilem's Avatar
    Join Date
    10-22-2011
    Location
    Ufa, Russia
    MS-Off Ver
    2013
    Posts
    3,377

    Re: How to make sure if every opening bracket has a closing one (with RegEx)

    Hi ImranBhatti,
    maybe something like this (withno RegEx)
    Please Login or Register  to view this content.

  3. #3
    Valued Forum Contributor ImranBhatti's Avatar
    Join Date
    03-27-2014
    Location
    Rawalpindi,Pakistan
    MS-Off Ver
    Office 365
    Posts
    1,785

    Re: How to make sure if every opening bracket has a closing one (with RegEx)

    Hi nilem
    Good move
    but it doesn't tells either "(" is missing or ")" is missing (and how many missing) because I need to complete the pairs as well.

  4. #4
    Forum Expert mikerickson's Avatar
    Join Date
    03-30-2007
    Location
    Davis CA
    MS-Off Ver
    Excel 2011
    Posts
    6,229

    Re: How to make sure if every opening bracket has a closing one (with RegEx)

    This is a little clunky, but it will tell you if a string has the parenthesis positioned correctly.

    Please Login or Register  to view this content.
    No routine can tell where a missing parenthesis goes. From your example:
    abc(xyz)...gsdfs(asdfasdfadfaf is missing a right paren.
    but is abc(xyz)...gsdfs(asdfasdfadfaf) the correct entry
    or is abc(xyz)...gsdfs(asdfasdf)adfaf
    _
    ...How to Cross-post politely...
    ..Wrap code by selecting the code and clicking the # or read this. Thank you.

  5. #5
    Forum Expert mikerickson's Avatar
    Join Date
    03-30-2007
    Location
    Davis CA
    MS-Off Ver
    Excel 2011
    Posts
    6,229

    Re: How to make sure if every opening bracket has a closing one (with RegEx)

    Another function you might use is

    Please Login or Register  to view this content.
    If ParenDifference is 0, there are matching left and right parenthesis. If ParenDifference is positive, then that is the number of excess left parenthesis. Negative is the number excess right parenthesis.

    Note that this is only the count of parenthesis and their position is not a factor, ParenDifference( "abc(abc))xy(") = 0

  6. #6
    Forum Expert shg's Avatar
    Join Date
    06-20-2007
    Location
    The Great State of Texas
    MS-Off Ver
    2010, 2019
    Posts
    40,689

    Re: How to make sure if every opening bracket has a closing one (with RegEx)

    Another way for the second:

    Please Login or Register  to view this content.
    Entia non sunt multiplicanda sine necessitate

  7. #7
    Forum Expert shg's Avatar
    Join Date
    06-20-2007
    Location
    The Great State of Texas
    MS-Off Ver
    2010, 2019
    Posts
    40,689

    Re: How to make sure if every opening bracket has a closing one (with RegEx)

    Another way for the first:

    Please Login or Register  to view this content.

  8. #8
    Forum Expert shg's Avatar
    Join Date
    06-20-2007
    Location
    The Great State of Texas
    MS-Off Ver
    2010, 2019
    Posts
    40,689

    Re: How to make sure if every opening bracket has a closing one (with RegEx)

    If you want to ignore embedded substrings (e.g., ")"(ab)c is fine), and also verify that there are an even number of quotes (e.g., ")"(ab)c" is not),

    Please Login or Register  to view this content.
    Last edited by shg; 02-03-2018 at 02:42 PM.

  9. #9
    Valued Forum Contributor ImranBhatti's Avatar
    Join Date
    03-27-2014
    Location
    Rawalpindi,Pakistan
    MS-Off Ver
    Office 365
    Posts
    1,785

    Re: How to make sure if every opening bracket has a closing one (with RegEx)

    Quote Originally Posted by mikerickson View Post

    No routine can tell where a missing parenthesis goes. From your example:
    abc(xyz)...gsdfs(asdfasdfadfaf is missing a right paren.
    but is abc(xyz)...gsdfs(asdfasdfadfaf) the correct entry
    or is abc(xyz)...gsdfs(asdfasdf)adfaf
    Hi Sir Mike
    Thanks for reviewing the thread
    The pattern is not important currently.What I need is to determine if there is ")" missing I will add it at the very end of the string and if "(" is less ,I will put it at the beginning of the string.
    I want to avoid loop here because there will be another loop on the cells in my range separately . I am afraid if I use loop within each cell and then for each cell my code will loose robustness. because there will cells in thousands

  10. #10
    Valued Forum Contributor ImranBhatti's Avatar
    Join Date
    03-27-2014
    Location
    Rawalpindi,Pakistan
    MS-Off Ver
    Office 365
    Posts
    1,785

    Re: How to make sure if every opening bracket has a closing one (with RegEx)

    Quote Originally Posted by mikerickson View Post
    Another function you might use is

    Please Login or Register  to view this content.
    If ParenDifference is 0, there are matching left and right parenthesis. If ParenDifference is positive, then that is the number of excess left parenthesis. Negative is the number excess right parenthesis.

    Note that this is only the count of parenthesis and their position is not a factor, ParenDifference( "abc(abc))xy(") = 0
    But ParenDifference("abc(xyz)...gsdfs(asdfasdfadfaf") = 1
    ParenDifference("abc(xyz)...gsdfs(asdfasdfadfaf)))")= -1

    Means

  11. #11
    Forum Guru
    Join Date
    08-15-2004
    Location
    Tokyo, Japan
    MS-Off Ver
    2013 O.365
    Posts
    22,834

    Re: How to make sure if every opening bracket has a closing one (with RegEx)

    Quote Originally Posted by ImranBhatti View Post
    Reason I don't want to use loop ,is because I will have to loop over cells in a range and if every cell has a sub loop then the code will go slow.
    Not really.
    It usually looping string would be faster than RegExp(it depends though).

    Anyway, your question is too rough.
    What about something like "(abc(xyz(123)ccc)((456)789)" ?

  12. #12
    Valued Forum Contributor ImranBhatti's Avatar
    Join Date
    03-27-2014
    Location
    Rawalpindi,Pakistan
    MS-Off Ver
    Office 365
    Posts
    1,785

    Re: How to make sure if every opening bracket has a closing one (with RegEx)

    OK....I further make it simple

    Can we get in a msgbox only the brackets

    i.e
    INPUT=(abc(xyz(123)ccc)((456)789)
    OUTPUT=((())(())

    Everything that is not "(" or is not ")" should be delected

  13. #13
    Forum Guru
    Join Date
    08-15-2004
    Location
    Tokyo, Japan
    MS-Off Ver
    2013 O.365
    Posts
    22,834

    Re: How to make sure if every opening bracket has a closing one (with RegEx)

    This is to give you number of ( or ) that have no pair if any.
    Please Login or Register  to view this content.

  14. #14
    Forum Guru xlnitwit's Avatar
    Join Date
    06-27-2016
    Location
    London
    MS-Off Ver
    Windows: 2010; Mac: 16.13 (O365)
    Posts
    7,085

    Re: How to make sure if every opening bracket has a closing one (with RegEx)

    Quote Originally Posted by mikerickson View Post
    Another function you might use is

    Please Login or Register  to view this content.
    FYI, there are two typos (highlighted).
    Don
    Please remember to mark your thread 'Solved' when appropriate.

  15. #15
    Valued Forum Contributor ImranBhatti's Avatar
    Join Date
    03-27-2014
    Location
    Rawalpindi,Pakistan
    MS-Off Ver
    Office 365
    Posts
    1,785

    Re: How to make sure if every opening bracket has a closing one (with RegEx)

    Hi jindon
    seems it will be helpful.but I am testing the likely combinations and let you know the outcome.

  16. #16
    Valued Forum Contributor ImranBhatti's Avatar
    Join Date
    03-27-2014
    Location
    Rawalpindi,Pakistan
    MS-Off Ver
    Office 365
    Posts
    1,785

    Re: How to make sure if every opening bracket has a closing one (with RegEx)

    Thanks mikericson and jindo

    and thanks xlnitwit for the spotting the typos in mike's solution.

  17. #17
    Forum Expert
    Join Date
    10-09-2014
    Location
    Newcastle, England
    MS-Off Ver
    2003 & 2013
    Posts
    1,986

    Re: How to make sure if every opening bracket has a closing one (with RegEx)

    Cant this just be done as a formula?

    Please Login or Register  to view this content.
    If someone has helped you then please add to their Reputation

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. How to make the multiple regex replace in one loop?
    By woshichuanqilz in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 04-05-2017, 11:53 PM
  2. [SOLVED] VBA editor automatically opening up after screen saver, laptop closing/opening etc...
    By clogistics in forum Excel Programming / VBA / Macros
    Replies: 11
    Last Post: 02-08-2016, 12:22 PM
  3. [SOLVED] Find a closing bracket in a cell
    By krishpraj in forum Excel Formulas & Functions
    Replies: 6
    Last Post: 03-27-2015, 07:19 AM
  4. Opening, Closing, and Opening Excel workbooks from MS Project
    By m007schneider in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 04-04-2013, 03:22 PM
  5. Replies: 1
    Last Post: 10-29-2012, 05:36 AM
  6. [SOLVED] Extracting a word to the left of an opening bracket
    By Glenn Kennedy in forum Excel General
    Replies: 5
    Last Post: 07-08-2012, 09:57 AM
  7. Trying to make a Bracket?
    By MD507 in forum Excel General
    Replies: 3
    Last Post: 02-28-2008, 09:31 PM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1