<p>I've seen lots of examples of making an entire regular expression case-insensitive.  What I'm wondering about is having just part of the expression be case-insensitive.</p>

<p>For example, let's say I have a string like this:</p>

<pre><code>fooFOOfOoFoOBARBARbarbarbAr
</code></pre>

<p>What if I want to match all occurrences of "foo" regardless of case but I only want to match the upper-case "BAR"s?</p>

<p>The ideal solution would be something that works across regex flavors but I'm interested in hearing language-specific ones as well (Thanks <a href="https://stackoverflow.com/questions/43632/can-you-make-just-part-of-a-regex-case-insensitive#43636">Espo</a>)</p>

<p><strong><em>Edit</em></strong></p>

<p>The link Espo provided was very helpful.  There's a good example in there about turning modifiers on and off within the expression.</p>

<p>For my contrived example, I can do something like this:</p>

<pre><code>(?i)foo*(?-i)|BAR
</code></pre>

<p>which makes the match case-insensitive for just the foo portion of the match.</p>

<p>That seemed to work in most regex implementations except Javascript, Python, and a few others (as Espo mentioned).  </p>

<p>The big ones that I was wondering about (Perl, PHP, .NET) all support inline mode changes.</p>

[{'answer_id': 43636, 'author': 'Espo', 'author_id': 2257, 'author_profile': 'https://Stackoverflow.com/users/2257', 'pm_score': 8, 'selected': True, 'text': '<p>Perl lets you make part of your regular expression case-insensitive by using the (?i:) pattern modifier.</p>\n\n<p>Modern regex flavors allow you to apply modifiers to only part of the regular expression. If you insert the modifier (?ism) in the middle of the regex, the modifier only applies to the part of the regex to the right of the modifier. You can turn off modes by preceding them with a minus sign. All modes after the minus sign will be turned off. E.g. (?i-sm) turns on case insensitivity, and turns off both single-line mode and multi-line mode.</p>\n\n<p>Not all regex flavors support this. JavaScript and Python apply all mode modifiers to the entire regular expression. They don\'t support the (?-ismx) syntax, since turning off an option is pointless when mode modifiers apply to the whole regular expressions. All options are off by default.</p>\n\n<p>You can quickly test how the regex flavor you\'re using handles mode modifiers. The regex (?i)te(?-i)st should match test and TEst, but not teST or TEST.</p>\n\n<p><a href="http://www.regular-expressions.info/modifiers.html" rel="noreferrer">Source</a></p>\n'}, {'answer_id': 43641, 'author': 'Kibbee', 'author_id': 1862, 'author_profile': 'https://Stackoverflow.com/users/1862', 'pm_score': 2, 'selected': False, 'text': "<p>You could use </p>\n\n<pre><code>(?:F|f)(?:O|o)(?:O|o)\n</code></pre>\n\n<p>The ?: in the brackets in .Net means it's non-capturing, and just used to group the terms of the | (or) statement.</p>\n"}, {'answer_id': 43654, 'author': 'akdom', 'author_id': 145, 'author_profile': 'https://Stackoverflow.com/users/145', 'pm_score': 3, 'selected': False, 'text': '<p>What language are you using?  A standard way to do this would be something like /([Ff][Oo]{2}|BAR)/ with case sensitivity on, but in Java, for example, there is a case sensitivity modifier (?i) which makes all characters to the right of it case insensitive and (?-i) which forces sensitivity.  An example of that Java regex modifier can be found <a href="http://exampledepot.com/egs/java.util.regex/Case.html" rel="noreferrer">here</a>.</p>\n'}, {'answer_id': 43655, 'author': 'aku', 'author_id': 1196, 'author_profile': 'https://Stackoverflow.com/users/1196', 'pm_score': 3, 'selected': False, 'text': '<p>Unfortunately syntax for case-insensitive matching is not common. \nIn .NET you can use RegexOptions.IgnoreCase flag or <strong>?i</strong> modifier</p>\n'}, {'answer_id': 58818125, 'author': 'Wiktor Stribiżew', 'author_id': 3832970, 'author_profile': 'https://Stackoverflow.com/users/3832970', 'pm_score': 4, 'selected': False, 'text': '<p>It is true one can rely on inline modifiers as described in <a href="https://www.regular-expressions.info/modifiers.html" rel="noreferrer"><em>Turning Modes On and Off for Only Part of The Regular Expression</em></a>:</p>\n<blockquote>\n<p>The regex <code>(?i)te(?-i)st</code> should match test and <code>TEst</code>, but not <code>teST</code> or <code>TEST</code>.</p>\n</blockquote>\n<p>However, a bit more supported feature is an <strong><code>(?i:...)</code> inline modifier group</strong> (see <a href="https://www.regular-expressions.info/modifiers.html" rel="noreferrer"><em>Modifier Spans</em></a>). The syntax is <code>(?i:</code>, then the pattern that you want to make cas-insensitive, and then a <code>)</code>.</p>\n<pre><code>(?i:foo)|BAR\n</code></pre>\n<p><strong>The reverse</strong>: If your pattern is compiled with a case insensitive option and you need to make a part of a regex case sensitive, you add <code>-</code> after <code>?</code>: <code>(?-i:...)</code>.</p>\n<p>Example uses in various languages (wrapping the matches with angle brackets):</p>\n<ul>\n<li><a href="/questions/tagged/php" class="post-tag" title="show questions tagged &#39;php&#39;" rel="tag">php</a> - <code>preg_replace(&quot;~(?i:foo)|BAR~&quot;, \'&lt;$0&gt;\', &quot;fooFOOfOoFoOBARBARbarbarbAr&quot;)</code> (<a href="https://3v4l.org/o25di" rel="noreferrer">demo</a>)</li>\n<li><a href="/questions/tagged/python" class="post-tag" title="show questions tagged &#39;python&#39;" rel="tag">python</a> - <code>re.sub(r\'(?i:foo)|BAR\', r\'&lt;\\g&lt;0&gt;&gt;\', \'fooFOOfOoFoOBARBARbarbarbAr\')</code> (<a href="https://ideone.com/53pLjc" rel="noreferrer">demo</a>) (note <a href="https://docs.python.org/3/library/re.html" rel="noreferrer">Python <code>re</code> supports</a> inline modifier groups since Python 3.6)</li>\n<li><a href="/questions/tagged/c%23" class="post-tag" title="show questions tagged &#39;c#&#39;" rel="tag">c#</a> / <a href="/questions/tagged/vb.net" class="post-tag" title="show questions tagged &#39;vb.net&#39;" rel="tag">vb.net</a> / <a href="/questions/tagged/.net" class="post-tag" title="show questions tagged &#39;.net&#39;" rel="tag">.net</a> - <code>Regex.Replace(&quot;fooFOOfOoFoOBARBARbarbarbAr&quot;, &quot;(?i:foo)|BAR&quot;, &quot;&lt;$&amp;&gt;&quot;)</code> (<a href="https://ideone.com/WO1bVv" rel="noreferrer">demo</a>)</li>\n<li><a href="/questions/tagged/java" class="post-tag" title="show questions tagged &#39;java&#39;" rel="tag">java</a> - <code>&quot;fooFOOfOoFoOBARBARbarbarbAr&quot;.replaceAll(&quot;(?i:foo)|BAR&quot;, &quot;&lt;$0&gt;&quot;)</code> (<a href="https://ideone.com/AHUrFx" rel="noreferrer">demo</a>)</li>\n<li><a href="/questions/tagged/perl" class="post-tag" title="show questions tagged &#39;perl&#39;" rel="tag">perl</a> - <code>$s =~ s/(?i:foo)|BAR/&lt;$&amp;&gt;/g</code> (<a href="https://ideone.com/4OewGe" rel="noreferrer">demo</a>)</li>\n<li><a href="/questions/tagged/ruby" class="post-tag" title="show questions tagged &#39;ruby&#39;" rel="tag">ruby</a> - <code>&quot;fooFOOfOoFoOBARBARbarbarbAr&quot;.gsub(/(?i:foo)|BAR/, \'&lt;\\0&gt;\')</code> (<a href="https://ideone.com/GZCbiI" rel="noreferrer">demo</a>)</li>\n<li><a href="/questions/tagged/r" class="post-tag" title="show questions tagged &#39;r&#39;" rel="tag">r</a> - <code>gsub(&quot;((?i:foo)|BAR)&quot;, &quot;&lt;\\\\1&gt;&quot;, &quot;fooFOOfOoFoOBARBARbarbarbAr&quot;, perl=TRUE)</code> (<a href="https://ideone.com/ipk4K4" rel="noreferrer">demo</a>)</li>\n<li><a href="/questions/tagged/swift" class="post-tag" title="show questions tagged &#39;swift&#39;" rel="tag">swift</a> - <code>&quot;fooFOOfOoFoOBARBARbarbarbAr&quot;.replacingOccurrences(of: &quot;(?i:foo)|BAR&quot;, with: &quot;&lt;$0&gt;&quot;, options: [.regularExpression])</code></li>\n<li><a href="/questions/tagged/go" class="post-tag" title="show questions tagged &#39;go&#39;" rel="tag">go</a> - (uses RE2) - <code>regexp.MustCompile(`(?i:foo)|BAR`).ReplaceAllString( &quot;fooFOOfOoFoOBARBARbarbarbAr&quot;, `&lt;${0}&gt;`)</code> (<a href="https://play.golang.org/p/e5iH26spNGo" rel="noreferrer">demo</a>)</li>\n</ul>\n<p>Not supported in <a href="/questions/tagged/javascript" class="post-tag" title="show questions tagged &#39;javascript&#39;" rel="tag">javascript</a>, <a href="/questions/tagged/bash" class="post-tag" title="show questions tagged &#39;bash&#39;" rel="tag">bash</a>, <a href="/questions/tagged/sed" class="post-tag" title="show questions tagged &#39;sed&#39;" rel="tag">sed</a>, <a href="/questions/tagged/c%2b%2b" class="post-tag" title="show questions tagged &#39;c++&#39;" rel="tag">c++</a> <code>std::regex</code>, <a href="/questions/tagged/lua" class="post-tag" title="show questions tagged &#39;lua&#39;" rel="tag">lua</a>, <a href="/questions/tagged/tcl" class="post-tag" title="show questions tagged &#39;tcl&#39;" rel="tag">tcl</a>.</p>\n<p>In these case, you can put both letter variants into a character class (not a group, see <a href="https://stackoverflow.com/q/22132450/3832970">Why is a character class faster than alternation?</a>). Examples:</p>\n<ul>\n<li><a href="/questions/tagged/sed" class="post-tag" title="show questions tagged &#39;sed&#39;" rel="tag">sed</a> <a href="/questions/tagged/posix-ere" class="post-tag" title="show questions tagged &#39;posix-ere&#39;" rel="tag">posix-ere</a> - <code>sed -E \'s/[Ff][Oo][Oo]|BAR/&lt;&amp;&gt;/g\' file &gt; outfile</code> (<a href="https://ideone.com/JrST8o" rel="noreferrer">demo</a>)</li>\n<li><a href="/questions/tagged/grep" class="post-tag" title="show questions tagged &#39;grep&#39;" rel="tag">grep</a> <a href="/questions/tagged/posix-ere" class="post-tag" title="show questions tagged &#39;posix-ere&#39;" rel="tag">posix-ere</a> - <code>grep -Eo \'[Ff][Oo][Oo]|BAR\' file</code> (or if you are using GNU grep, you can still use the PCRE regex, <code>grep -Po \'(?i:foo)|BAR\' file</code> (<a href="https://ideone.com/i6JRbL" rel="noreferrer">demo</a>))</li>\n</ul>\n'}]
['https://Stackoverflow.com/questions/43632', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/305/']