<p>I am working on a C++ code base that was recently moved from X/Motif to Qt.  I am trying to write a Perl script that will replace all occurrences of Boolean (from X) with bool.  The script just does a simple replacement.  </p>

<pre><code>s/\bBoolean\b/bool/g
</code></pre>

<p>There are a few conditions.   </p>

<p>1) We have CORBA in our code and \b matches CORBA::Boolean which should <strong>not</strong> be changed.<br>
2) It should not match if it was found as a string (i.e. "Boolean")</p>

<p><strong>Updated:</strong></p>

<p>For #1, I used lookbehind</p>

<pre><code>s/(?&lt;!:)\bBoolean\b/bool/g;
</code></pre>

<p>For #2, I used lookahead.</p>

<pre><code>s/(?&lt;!:)\bBoolean\b(?!")/bool/g&lt;/pre&gt;
</code></pre>

<p>This will most likely work for my situation but how about the following improvements?</p>

<p>3) Do not match if in the middle of a string (thanks <a href="https://stackoverflow.com/users/3101/nohat">nohat</a>).<br>
4) Do not match if in a comment.  (// or /**/) </p>

[{'answer_id': 35198, 'author': 'John Meagher', 'author_id': 3535, 'author_profile': 'https://Stackoverflow.com/users/3535', 'pm_score': 0, 'selected': False, 'text': '<p>To fix condition 1 try:</p>\n\n<pre><code>s/[^:]\\bBoolean\\b(?!")/bool/g\n</code></pre>\n\n<p>The [^:] says to match any character other than ":".  </p>\n'}, {'answer_id': 35203, 'author': 'Daniel Jennings', 'author_id': 3641, 'author_profile': 'https://Stackoverflow.com/users/3641', 'pm_score': 1, 'selected': False, 'text': '<pre><code>s/[^:]\\bBoolean\\b[^"]/bool/g\n</code></pre>\n\n<p>Edit: Rats, beaten again. +1 for beating me, good sir.</p>\n'}, {'answer_id': 35220, 'author': 'KannoN', 'author_id': 2897, 'author_profile': 'https://Stackoverflow.com/users/2897', 'pm_score': 2, 'selected': False, 'text': '<blockquote>\n  <p>s/[^:]\\bBoolean\\b(?!")/bool/g</p>\n</blockquote>\n\n<p>This does not match strings where Boolean is at that the beginning of the line becuase [^:] is "match a character that is not :".</p>\n'}, {'answer_id': 35358, 'author': 'nohat', 'author_id': 3101, 'author_profile': 'https://Stackoverflow.com/users/3101', 'pm_score': 2, 'selected': False, 'text': "<p>Watch out with that quote-matching lookahead assertion. That'll only match if <em>Boolean</em> is the last part of a string, but not in the middle of the string. You'll need to match an even number of quote marks preceding the match if you want to be sure you're not in a string (assuming no multi-line strings and no escaped embedded quote marks).</p>\n"}, {'answer_id': 43729, 'author': 'Jagmal', 'author_id': 4406, 'author_profile': 'https://Stackoverflow.com/users/4406', 'pm_score': 0, 'selected': False, 'text': '<blockquote>\n  <p>3) Do not match if in the middle of a string (thanks nohat).</p>\n</blockquote>\n\n<p>You can perhaps write a reg ex to check ".*Boolean.*". But what if you have quote(") inside the string? So, you have more work to not exclude (\\") pattern. </p>\n\n<blockquote>\n  <p>4) Do not match if in a comment. (// or /* */)</p>\n</blockquote>\n\n<p>For \'//\', you can have a regex to exclude //.* But, better could be to first put a regex to compare the whole line for the // comments ((.*)(//.*)) and then apply replacement only on $1 (first matching pattern).</p>\n\n<p>For /* */, it is more complex as this is multiline pattern. One approach can be to first run whole of you code to match multiline comments and then take out only the parts not matching ... something like ... (.*)(/*.**/)(.*). But, the actual regex would be even more complex as you would have not one but more of multi-line comments.</p>\n\n<p>Now, what if you have /* or */ inside // block? (I dont know why would you have it.. but Murphy\'s law says that you can have it). There is obviously some way out but my idea is to emphasize how bad-looking the regex will become.</p>\n\n<p>My suggestion here would be to use some lexical tool for C++ and replace the token Boolean with bool. Your thoughts?</p>\n'}, {'answer_id': 86617, 'author': 'piCookie', 'author_id': 8763, 'author_profile': 'https://Stackoverflow.com/users/8763', 'pm_score': 0, 'selected': False, 'text': "<p>In order to avoid writing a full C parser in perl, you're trying to strike a balance. Depending on how much needs changing, I would be inclined to do something like a very restrictive s/// and then anything that still matches /Boolean/ gets written to an exception file for human decision making. That way you're not trying to parse the C middle strings, multi-line comment, conditional compiled out text, etc. that could be present.</p>\n"}, {'answer_id': 86920, 'author': 'Aristotle Pagaltzis', 'author_id': 9410, 'author_profile': 'https://Stackoverflow.com/users/9410', 'pm_score': 0, 'selected': False, 'text': '<blockquote>\n  <ol>\n  <li>…</li>\n  <li>…</li>\n  <li>Do not match if in the middle of a string (thanks nohat).</li>\n  <li>Do not match if in a comment. (// or /**/)</li>\n  </ol>\n</blockquote>\n\n<p>No can do with a <em>simple</em> regex. For that, you need to actually look at <em>every single</em> character left-to-right and decide what kind of thing it is, at least well enough to tell apart comments from multi-line comments from strings from other stuff, and <em>then</em> you need to see if the “other stuff” part contains things you want to change.</p>\n\n<p>Now, I don’t know the <strong>exact</strong> syntactical rules for comments and strings in C++ so the following is going to be imprecise and completely undebugged, but it’ll give you an idea of the complexity you’re up against.</p>\n\n<pre><code>my $line_comment      = qr! (?&gt; // .* \\n? ) !x;\nmy $multiline_comment = qr! (?&gt; /\\* [^*]* (?: \\* (?: [^/*] [^*]* )? )* )* \\*/ ) !x;\nmy $string            = qr! (?&gt; " [^"\\\\]* (?: \\\\ . [^"\\\\]* )* " ) !x;\nmy $boolean_type      = qr! (?&lt;!:) \\b Boolean \\b !x;\n\n$code =~ s{ \\G (\n      $line_comment\n    | $multiline_comment\n    | $string\n    | ( $boolean_type )\n    | .\n) }{\n    defined $2 ? \'bool\' : $1\n}gex;\n</code></pre>\n\n<p>Please don’t ask me to explain this in all its intricacies, it would take me a day and another. Just buy and read Jeff&#160;Friedl’s <a href="http://regex.info/" rel="nofollow noreferrer"><i>Mastering Regular Expressions</i></a> if you want to understand exactly what is going on here.</p>\n'}, {'answer_id': 88938, 'author': 'Victor', 'author_id': 14514, 'author_profile': 'https://Stackoverflow.com/users/14514', 'pm_score': 0, 'selected': False, 'text': '<p>The "\'Boolean\' in the middle of a string" part sounds a bit unlikely, I\'d check first if there is any occurrence of it in the code with something like</p>\n\n<pre><code>m/"[^"]*Boolean[^"]*"/\n</code></pre>\n\n<p>And if there is none or a few, just ignore that case.</p>\n'}, {'answer_id': 95149, 'author': 'nimish', 'author_id': 3926, 'author_profile': 'https://Stackoverflow.com/users/3926', 'pm_score': 1, 'selected': False, 'text': "<pre><code>#define Boolean bool\n</code></pre>\n\n<p>Let the preprocesser take care of this. Every time you see a Boolean you can either manually fix it or hope a regex doesn't make a mistake. Depending on how many macros you use you can you could dump the out of cpp.</p>\n"}]
['https://Stackoverflow.com/questions/35178', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/2897/']