Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>I think you meant "match on a string that starts with <code>--</code> and is not <del>proceeded</del>preceeded by <code>'</code> and not followed by <code>'</code> with any number of characters inbetween"</p> <p>If so, then this is what you are looking for:</p> <pre><code>string = Regex.Replace(cmd, "(?&lt;!'.*?--)--(?!.*?').*(?=\r\n)", "") 'EDIT: modified a little </code></pre> <p>Of course, it means you can't have apostrophes in your comments... and would be exceedingly easy to hack if someone wanted to (you aren't thinking of using this to protect against injection attacks, are you? <strong><em>ARE YOU!??!</em> &nbsp;:D</strong> )</p> <p>I can break down the expression if you'd like, but it's essentially the same as my modified quote above!</p> <p><strong>EDIT:</strong></p> <p>I modified the expression a little, so it does not consume any carriage return, only the comment itself... the expression says:</p> <pre><code>(?&lt;! # negative lookbehind assertion* ' # match a literal single quote .*? # followed by anything (reluctantly*) -- # two literal dashes ) # end assertion -- # match two literal dashes (?! # negative lookahead assertion .*? # match anything (reluctant) ' # followed by a literal single quote ) # end assertion .* # match anything (?= # positive lookahead assertion \r\n # match carriage-return, line-feed ) # end assertion </code></pre> <ul> <li><em>negative lookbehind assertion</em> means at this point in the match, look backward here and assert that this cannot be matched</li> <li><em>negative lookahead assertion</em> means look forward from this point and assert this cannot be matched</li> <li><em>positive lookahead</em> asserts the following expression CAN be matched</li> <li><em>reluctant</em> means only consume a match for the previous atom (the <code>.</code> which means everything in this case) if you cannot match the expression that follows. Thus the <code>.*?</code> in <code>.*?--</code> (when applied against the string <code>abc--</code>) will consume <code>a</code>, then check to see if the <code>--</code> can be matched and fail; it will then consume <code>ab</code>, but stop again to see if the <code>--</code> can be matched and fail; once it consumes <code>abc</code> and the <code>--</code> can be matched (success), it will finally consume the entire <code>abc--</code> <ul> <li><em>non-reluctant or "greedy"</em> which would be <code>.*</code> without the <code>?</code> will match <code>abc--</code> with the <code>.*</code>, then try to match the end of the string with <code>--</code> and fail; it will then <em>backtrack</em> until it can match the <code>--</code></li> </ul></li> <li>one additional note is that the <code>.</code> "anything" does not by default include newlines (carriage-return/line-feed), which is needed for this to work properly (there is a switch that will allow <code>.</code> to match newlines and it will break this expression)</li> </ul> <p>A good resource - where I've learned 90% of what I know about regex - is <a href="http://regular-expressions.info/" rel="nofollow">Regular-Expressions.info</a></p> <p>Tread carefully and good luck!</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload