• Skip to main content
  • Skip to search
  • Skip to footer
Cadence Home
  • This search text may be transcribed, used, stored, or accessed by our third-party service providers per our Cookie Policy and Privacy Policy.

  1. Community Forums
  2. Custom IC SKILL
  3. Code to strip outer parentheses

Stats

  • Locked Locked
  • Replies 8
  • Subscribers 143
  • Views 17076
  • Members are here 0
This discussion has been locked.
You can no longer post new replies to this discussion. If you have a question you can start a new discussion

Code to strip outer parentheses

CADcasualty
CADcasualty over 7 years ago

Hi,

I am calling a routine that returns either a number or a string (the routine is figuring out a parent parameter defined in some upper parent in the hierarchy). The problem is that if a string is returned it is surrounded by a set of parentheses for every hierarchical level that was traversed finding the parent parameter. So the routine could return a straight number, or it could return some text surrounded by an arbitrary amount of parentheses e.g. "blah" or "(blah)" or "((((((blah))))))".

I can't edit the original routine and I'm stumped trying to figure out a way to remove all outer pairs of parentheses - can anybody point me to some code that does this. Please note that I only want to remove OUTER pairs of parentheses i.e. if I had "(((blah(0))))" then I'd want to end up with "blah(0)".

Many thanks in advance.

  • Cancel
Parents
  • skillUser
    skillUser over 7 years ago

    Hi,

    I think this is what you wanted :

    procedure(removeParens(item)
      cond(
        (stringp(item) removeParens(readstring(item)))
        (listp(item) mapcan('removeParens item))
        (t list(item))
      )
    )

    Results:

    str = "((((((blah))))))"
    removeParens(str)
    => (blah)

    str = "((((((blah(0)))))))"
    removeParens(str)
    => (blah 0)

    You could check to see if there is a cdr() of the returned result, if so return the whole result, otherwise return the car of the result:

    result = removeParens(str)
    if(cdr(result) result car(result))
    => blah OR (blah 0)


    Hopefully this helps?

    Regards,

    Lawrence.

    • Cancel
    • Vote Up 0 Vote Down
    • Cancel
  • CADcasualty
    CADcasualty over 7 years ago in reply to skillUser

    Thanks Lawrence - this is almost what I want! I need matching pairs of OUTER parentheses removed. Specifically:

    if I had "(((blah(0))))" then I'd want to end up with "blah(0)"

    • Cancel
    • Vote Up 0 Vote Down
    • Cancel
  • Andrew Beckett
    Andrew Beckett over 7 years ago in reply to CADcasualty

    I'd use a pattern matching approach rather than relying on the SKILL parser. Otherwise if you have unusual characters in there, Lawrence's code would probably get a bit upset. Something like this would work:

    procedure(CCFremoveOuterParens(str)
      unless(CCFremoveOuterParens.pat
        CCFremoveOuterParens.pat=pcreCompile("^\\((.*)\\)$")
      )
      if(pcreExecute(CCFremoveOuterParens.pat str) then
        CCFremoveOuterParens(pcreSubstitute(CCFremoveOuterParens.pat "\\1"))
      else
        str
      )
    )
    • Cancel
    • Vote Up 0 Vote Down
    • Cancel
Reply
  • Andrew Beckett
    Andrew Beckett over 7 years ago in reply to CADcasualty

    I'd use a pattern matching approach rather than relying on the SKILL parser. Otherwise if you have unusual characters in there, Lawrence's code would probably get a bit upset. Something like this would work:

    procedure(CCFremoveOuterParens(str)
      unless(CCFremoveOuterParens.pat
        CCFremoveOuterParens.pat=pcreCompile("^\\((.*)\\)$")
      )
      if(pcreExecute(CCFremoveOuterParens.pat str) then
        CCFremoveOuterParens(pcreSubstitute(CCFremoveOuterParens.pat "\\1"))
      else
        str
      )
    )
    • Cancel
    • Vote Up 0 Vote Down
    • Cancel
Children
No Data

Community Guidelines

The Cadence Design Communities support Cadence users and technologists interacting to exchange ideas, news, technical information, and best practices to solve problems and get the most from Cadence technology. The community is open to everyone, and to provide the most value, we require participants to follow our Community Guidelines that facilitate a quality exchange of ideas and information. By accessing, contributing, using or downloading any materials from the site, you agree to be bound by the full Community Guidelines.

© 2025 Cadence Design Systems, Inc. All Rights Reserved.

  • Terms of Use
  • Privacy
  • Cookie Policy
  • US Trademarks
  • Do Not Sell or Share My Personal Information