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

18989[Clip] Re: Using regex to insert a comment

Expand Messages
  • Flo
    Mar 5, 2009
      --- In ntb-clips@yahoogroups.com, "Don - HtmlFixIt.com" <don@...> wrote:
      >
      > Um yes Flo, can you break that down for the slow kids in the
      > back here?

      Here we go...

      (?s) = "dot matches all" modifier (also NL to match multiple lines)

      ^<!-- = matches at start of a comment line

      [^-]+ = character class: any character that is no "-" (i.e. the body of comment)

      -->\x20\r\n = matches at end of comment line, followed by a space (in hex), and a CRNL

      <li> = matches at start of an added line (to identify added lines following a comment line)

      .+?((?=^<!)|\Z) = matches one or more (non-greedy) following added lines up to the next comment line using a Positive Lookahead Assertion (?=^<!) embedded in an alternation: matches either before "^<!", i.e. the start of a comment line, or at end of list (\Z) if last line is an added line.

      The second RegEx (in ^$GetDocMatchAll$)...

      ^<\!--\x20\K([^-]+)(?=-->\x20\r\n<li>)

      matches the body of a comment line only:

      ^<\!--\x20\K = matches at start of comment line (obviously the escaping of the exclamation mark isn't necessary here), followed by a space (in hex) but, with \K, doesn't select the match. Since ^$GetDocMatchAll$ searches only within the selection made by ^!Find, it captures the body in that block only (cf the "CRS1" used by Paul to capture the first subpattern only).

      I love to play with that different solutions. I also like Pauls approach. Maybe it's a little bit slower with very large lists because every ^!Find and ^!Jump slows down a clip whereas those ^$GetDoc-Functions are extremely fast.

      But in the end it doesn't matter very much -- "many roads lead to Rome" ;-)

      Regards,
      Flo

      P.S. My clip (revised):

      ^!SetScreenUpdate Off
      ^!Jump Doc_Start
      :Start
      ^!Find "(?s)^<!--[^-]+-->\x20\r\n<li>.+?((?=^<!)|\Z)" RS
      ^!IfError End
      ^!SetClipboard ^$GetDocMatchAll("^<!--\x20\K([^-]+)(?=-->\x20\r\n<li>)")$
      ^!Replace "(<li>)(<b>)" >> "$1^$GetClipboard$$2" HARS
      ^!Jump Select_End
      ^!Goto Start

      Please note when testing it with Paul's big list: This list is processed, i.e. it contains those bodies already; also, you have to remove a dozen CRNL inserted by this forum. That is, all lines have to start either with "<!--" or "<li><b>" (when word wrap is off).
    • Show all 15 messages in this topic