<p>I've been trying to figure out a regex to allow me to search for a particular string while automatically skipping comments.  Anyone have an RE like this or know of one?  It doesn't even need to be sophisticated enough to skip <code>#if 0</code> blocks; I just want it to skip over <code>//</code> and <code>/*</code> blocks.  The converse, that is only search inside comment blocks, would be very useful too. </p>

<p>Environment: VS 2003</p>

[{'answer_id': 54056, 'author': 'Ed Guiness', 'author_id': 4200, 'author_profile': 'https://Stackoverflow.com/users/4200', 'pm_score': 3, 'selected': True, 'text': '<p>This is a harder problem than it might at first appear, since you need to consider comment tokens inside strings, comment tokens that are themselves commented out etc.</p>\n\n<p>I wrote a string and comment parser for C#, let me see if I can dig out something that will help... I\'ll update if I find anything.</p>\n\n<p>EDIT:\n... ok, so I found my old \'codemasker\' project.  Turns out that I did this in stages, not with a single regex.  Basically I inch through a source file looking for start tokens, when I find one I then look for an end-token and mask everything in between.  This takes into account the context of the start token... if you find a token for "string start" then you can safely ignore comment tokens until you find the end of the string, and vice versa. Once the code is masked (I used guids as masks, and a hashtable to keep track) then you can safely do your search and replace, then finally restore the masked code.</p>\n\n<p>Hope that helps.</p>\n'}, {'answer_id': 54148, 'author': 'Jason Cohen', 'author_id': 4926, 'author_profile': 'https://Stackoverflow.com/users/4926', 'pm_score': 2, 'selected': False, 'text': '<p>Be especially careful with strings.  Strings often have escape sequences which you also have to respect while you\'re finding the end of them.</p>\n\n<p>So e.g. <code>"This is \\"a test\\""</code>.  You cannot blindly look for a double-quote to terminate.  Also beware of ``"This is \\"`, which shows that you cannot just say "unless double-quote is preceded by a backslash."</p>\n\n<p>In summary, make some brutal unit tests!</p>\n'}, {'answer_id': 54205, 'author': 'asksol', 'author_id': 5577, 'author_profile': 'https://Stackoverflow.com/users/5577', 'pm_score': 1, 'selected': False, 'text': '<p>I would make a copy and strip out the comments first, then search the string the regular way.</p>\n'}, {'answer_id': 55604, 'author': 'jfs', 'author_id': 4279, 'author_profile': 'https://Stackoverflow.com/users/4279', 'pm_score': 2, 'selected': False, 'text': '<p>A regexp is not the best tool for the job.</p>\n\n<p><a href="http://perldoc.perl.org/perlfaq6.html#How-do-I-use-a-regular-expression-to-strip-C-style-comments-from-a-file%3f" rel="nofollow noreferrer">Perl FAQ</a>:</p>\n\n<p>C comments:</p>\n\n<pre><code>#!/usr/bin/perl\n$/ = undef;\n$_ = &lt;&gt;; \n\ns#/\\*[^*]*\\*+([^/*][^*]*\\*+)*/|([^/"\']*("[^"\\\\]*(\\\\[\\d\\D][^"\\\\]*)*"[^/"\']*|\'[^\'\\\\]*(\\\\[\\d\\D][^\'\\\\]*)*\'[^/"\']*|/+[^*/][^/"\']*)*)#$2#g;\nprint; \n</code></pre>\n\n<p>C++ comments:</p>\n\n<pre><code>#!/usr/local/bin/perl\n$/ = undef;\n$_ = &lt;&gt;;\n\ns#//(.*)|/\\*[^*]*\\*+([^/*][^*]*\\*+)*/|"(\\\\.|[^"\\\\])*"|\'(\\\\.|[^\'\\\\])*\'|[^/"\']+#  $1 ? "/*$1 */" : $&amp; #ge;\nprint;\n</code></pre>\n'}]
['https://Stackoverflow.com/questions/54047', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/2820/']