On Wed, Oct 27, 2010 at 08:58:37PM -0700, Govinda wrote: > Hi everyone > > Amidst all the excitement for the new v.9.6, I hope there is time for > my grep Q: > > I am trying to make a grep search string which finds any HTML comment > that has a double hyphen inside itself. > > It should find any HTML comment which: > - starts with "<!--", > - ends with "-->", > - spans any number of lines, > - contains two consecutive hyphen chars which are not part of the > opening "<!--" or closing "-->". > > It should NOT match across multiple HTML comments. > > The find string must preserve anything it finds inside the HTML > comment which is not the double hyphen.. so that I can replace in a > way to just effectively strip out the double hyphen.
This will find HTML comments that contain exactly one occurence of -- in the middle of the comment and strip it out: Find (?s)(<!--(?>(?:(?!--).)*))--(?!>)((?>(?:(?!--).)*)-->) Replace \1\2 This will find HTML comments that contain at least one occurence of --, and strip out the first one; you could run it multiple times to strip them all: Find (?s)(<!--(?>(?:(?!--).)*))--(?!>)(.*?-->) Replace \1\2 If you want to strip an arbitrary number of occurences of -- within each comment, that's not possible with a single grep, because you need a loop within a loop. (?>...) means don't backtrack within the subexpression. In this case I know backtracking couldn't result in a match, so it would be a waste of time to try it. (?:(?!--).)* means match any number of characters, as long as there's no --; in other words, it matches up to the next --. Note that neither of these regexes will work as intended if the file contains comments like "<!----->" or "<!-- hello world --->". Or even "<!------> hello world -->". Hopefully you don't have anything like that in your file. :) Ronald -- You received this message because you are subscribed to the "BBEdit Talk" discussion group on Google Groups. To post to this group, send email to [email protected] To unsubscribe from this group, send email to [email protected] For more options, visit this group at <http://groups.google.com/group/bbedit?hl=en> If you have a feature request or would like to report a problem, please email "[email protected]" rather than posting to the group. Follow @bbedit on Twitter: <http://www.twitter.com/bbedit>
