18989[Clip] Re: Using regex to insert a comment
- Mar 5, 2009--- In firstname.lastname@example.org, "Don - HtmlFixIt.com" <don@...> wrote:
>Here we go...
> Um yes Flo, can you break that down for the slow kids in the
> back here?
(?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$)...
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" ;-)
P.S. My clip (revised):
^!Find "(?s)^<!--[^-]+-->\x20\r\n<li>.+?((?=^<!)|\Z)" RS
^!Replace "(<li>)(<b>)" >> "$1^$GetClipboard$$2" HARS
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).
- << Previous post in topic Next post in topic >>