Loading ...
Sorry, an error occurred while loading the content.

A little help with parentheses

Expand Messages
  • John Shotsky
    I m trying to write a clip that matches up the ( and ) parenthesis sets in a document and identifies any opening paren that doesn t have a matching close paren
    Message 1 of 3 , Sep 21, 2010
    • 0 Attachment
      I'm trying to write a clip that matches up the ( and ) parenthesis sets in a document and identifies
      any opening paren that doesn't have a matching close paren later, possibly several lines later. The
      problem I have is that sometimes, there is a paragraph number with a ) in it, and I want to ignore
      those. Also, there can be intervening sets of open and close parens.

      So, my users will click this clip to find out if there are any opening parens with missing closing
      parens. The closing parens in paragraph numbers will be ignored. The reason for this is that one of
      my other tools looks multi-line to close any open parens to 'unwrap' lines that may have hard CR's
      in them from email, etc.
      ;=====
      Sample paragraph 1: (this is the opening paren
      Followed by a close paren here.) This would be ignored, and later, the 'closer' clip would pull
      these lines back together using the parens. I have that code.

      Sample paragraph 2: (This is the opening paren for paragraph 2. This one doesn't actually have a
      closing paren in the rest of the document, so it is the one to be detected.

      1.) This is some text that is between the opening and closing paren. The closing paren here should
      be ignored, because it is part of a paragraph number. (These opening and closing parens need to be
      matched to each other, but not to the opening paren in paragraph 2.)

      This is the closing paren for Sample paragraph 2, except that the paren is missing, and that's what
      needs to be detected.

      This is just another sentence.

      This is another sentence with both an opening and closing paren within it. (Neither of these parens
      should match up with the missing close paren for paragraph 2.)
      ;=====
      So, when a user uses the clip, it should highlight starting at the opening paren of paragraph 2, and
      highlight to the end of the file, if a closing paren for that opening paren is not found. It would
      also be enough to just highlight the paren that doesn't have a closing paren, so the user could
      inspect to see where to place the missing closing paren manually. The problem is that the automated
      paren closer will close everything, causing errors. It would be sufficient just to find the first
      paren that doesn't have a closing paren too, then they could fix that one, and try again. There
      aren't many of these problems, but at present there is no way to 'pre-inspect' to see if this
      problem exists. (I'll probably write the routine into my master clip library to just precheck the
      file and abort if an error condition is found.)

      I've tried quite a few things already, but I guess I still don't have the skill to work this one
      out. I get stuck in a dumb loop sometimes, and simply can't break out of it. That usually means I've
      not approached the problem right in the first place, and will never find an answer on my own. You
      guys/gals always have good ideas.

      Thanks for any help,
      John


      [Non-text portions of this message have been removed]
    • diodeom
      ... To just flag any orphaned opening parentheses for editing (and to avoid a really convoluted regex), I d probably temporarily mark all the legitimate ones
      Message 2 of 3 , Sep 21, 2010
      • 0 Attachment
        John Shotsky wrote:
        >
        > I'm trying to write a clip that matches up the ( and ) parenthesis sets in a document and identifies
        > any opening paren that doesn't have a matching close paren later, possibly several lines later. The
        > problem I have is that sometimes, there is a paragraph number with a ) in it, and I want to ignore
        > those. Also, there can be intervening sets of open and close parens.
        >
        > So, my users will click this clip to find out if there are any opening parens with missing closing
        > parens. The closing parens in paragraph numbers will be ignored. The reason for this is that one of
        > my other tools looks multi-line to close any open parens to 'unwrap' lines that may have hard CR's
        > in them from email, etc.
        > ;=====
        > Sample paragraph 1: (this is the opening paren
        > Followed by a close paren here.) This would be ignored, and later, the 'closer' clip would pull
        > these lines back together using the parens. I have that code.
        >
        > Sample paragraph 2: (This is the opening paren for paragraph 2. This one doesn't actually have a
        > closing paren in the rest of the document, so it is the one to be detected.
        >
        > 1.) This is some text that is between the opening and closing paren. The closing paren here should
        > be ignored, because it is part of a paragraph number. (These opening and closing parens need to be
        > matched to each other, but not to the opening paren in paragraph 2.)
        >
        > This is the closing paren for Sample paragraph 2, except that the paren is missing, and that's what
        > needs to be detected.
        >
        > This is just another sentence.
        >
        > This is another sentence with both an opening and closing paren within it. (Neither of these parens
        > should match up with the missing close paren for paragraph 2.)
        > ;=====
        > So, when a user uses the clip, it should highlight starting at the opening paren of paragraph 2, and
        > highlight to the end of the file, if a closing paren for that opening paren is not found. It would
        > also be enough to just highlight the paren that doesn't have a closing paren, so the user could
        > inspect to see where to place the missing closing paren manually.


        To just flag any orphaned opening parentheses for editing (and to avoid a really convoluted regex), I'd probably temporarily mark all the "legitimate" ones first. See if this works for you:

        ;Mark oddball ")" when a part of a paragraph number
        ^!Replace "^\d+\.\K\)" >> "*" WARS
        ;Mark all properly closed parenthesized sections
        ^!Replace "\(([^()]++)\)" >> "*@$1@*" WARS
        ;IF NESTED, locate and mark their parent parens
        ^!IfError Next Else Skip_-1
        ;Set flags for orphaned opening parens
        ^!Replace "(?=\()" >> "==> " WARS
        ;Restore previously marked "okay" parens
        ^!Replace "*@" >> "(" WAS
        ^!Replace "\x40\x2A|\x2A" >> ")" WARS
        ;Locate flagged orphans for manual editing
        ^!Find "==> (" WS
        ^!Toolbar Find Text
      • John Shotsky
        Dio, That is very clever. Yes, I think that will work. I haven t actually tried it yet, but I understand it. I also use substitution tokens and find them
        Message 3 of 3 , Sep 22, 2010
        • 0 Attachment
          Dio,

          That is very clever. Yes, I think that will work. I haven't actually tried it yet, but I understand
          it. I also use substitution tokens and find them invaluable. In fact, it occurred to me that the
          close parens in paragraph numbers are actually removed by my code anyway, so I'll just run the
          paragraph number clip first, and use your code for what remains. Again, I was in a dumb loop, not
          realizing that I should get rid of the unwanted parens entirely, THEN make my test. That's what's
          great about this group - different folks approach things in entirely different, and often better,
          ways. I've learned a lot from the 'team'.

          Thanks!
          John

          From: ntb-clips@yahoogroups.com [mailto:ntb-clips@yahoogroups.com] On Behalf Of diodeom
          Sent: Tuesday, September 21, 2010 13:52
          To: ntb-clips@yahoogroups.com
          Subject: [Clip] Re: A little help with parentheses


          John Shotsky wrote:
          >
          > I'm trying to write a clip that matches up the ( and ) parenthesis sets in a document and
          identifies
          > any opening paren that doesn't have a matching close paren later, possibly several lines later.
          The
          > problem I have is that sometimes, there is a paragraph number with a ) in it, and I want to ignore
          > those. Also, there can be intervening sets of open and close parens.
          >
          > So, my users will click this clip to find out if there are any opening parens with missing closing
          > parens. The closing parens in paragraph numbers will be ignored. The reason for this is that one
          of
          > my other tools looks multi-line to close any open parens to 'unwrap' lines that may have hard CR's
          > in them from email, etc.
          > ;=====
          > Sample paragraph 1: (this is the opening paren
          > Followed by a close paren here.) This would be ignored, and later, the 'closer' clip would pull
          > these lines back together using the parens. I have that code.
          >
          > Sample paragraph 2: (This is the opening paren for paragraph 2. This one doesn't actually have a
          > closing paren in the rest of the document, so it is the one to be detected.
          >
          > 1.) This is some text that is between the opening and closing paren. The closing paren here should
          > be ignored, because it is part of a paragraph number. (These opening and closing parens need to be
          > matched to each other, but not to the opening paren in paragraph 2.)
          >
          > This is the closing paren for Sample paragraph 2, except that the paren is missing, and that's
          what
          > needs to be detected.
          >
          > This is just another sentence.
          >
          > This is another sentence with both an opening and closing paren within it. (Neither of these
          parens
          > should match up with the missing close paren for paragraph 2.)
          > ;=====
          > So, when a user uses the clip, it should highlight starting at the opening paren of paragraph 2,
          and
          > highlight to the end of the file, if a closing paren for that opening paren is not found. It would
          > also be enough to just highlight the paren that doesn't have a closing paren, so the user could
          > inspect to see where to place the missing closing paren manually.

          To just flag any orphaned opening parentheses for editing (and to avoid a really convoluted regex),
          I'd probably temporarily mark all the "legitimate" ones first. See if this works for you:

          ;Mark oddball ")" when a part of a paragraph number
          ^!Replace "^\d+\.\K\)" >> "*" WARS
          ;Mark all properly closed parenthesized sections
          ^!Replace "\(([^()]++)\)" >> "*@$1@*" WARS
          ;IF NESTED, locate and mark their parent parens
          ^!IfError Next Else Skip_-1
          ;Set flags for orphaned opening parens
          ^!Replace "(?=\()" >> "==> " WARS
          ;Restore previously marked "okay" parens
          ^!Replace "*@" >> "(" WAS
          ^!Replace "\x40\x2A|\x2A" >> ")" WARS
          ;Locate flagged orphans for manual editing
          ^!Find "==> (" WS
          ^!Toolbar Find Text



          [Non-text portions of this message have been removed]
        Your message has been successfully submitted and would be delivered to recipients shortly.