<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>The Original Jelani Harris &#187; java</title>
	<atom:link href="http://jelaniharris.com/tag/java/feed/" rel="self" type="application/rss+xml" />
	<link>http://jelaniharris.com</link>
	<description>The original website of Jelani Harris the original of course</description>
	<lastBuildDate>Sat, 24 Sep 2011 18:58:13 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>Case-insensitive replaceAll in Java</title>
		<link>http://jelaniharris.com/2009/case-insensitive-replaceall-in-java/</link>
		<comments>http://jelaniharris.com/2009/case-insensitive-replaceall-in-java/#comments</comments>
		<pubDate>Thu, 10 Sep 2009 17:54:11 +0000</pubDate>
		<dc:creator>Jelani Harris</dc:creator>
				<category><![CDATA[Code Gems]]></category>
		<category><![CDATA[java]]></category>
		<category><![CDATA[regex]]></category>

		<guid isPermaLink="false">http://jelaniharris.com/?p=67</guid>
		<description><![CDATA[The replaceAll function in the java.lang.String class replaces each substring found in that matches the regular expression to replace. String sentence = &#34;The sly brown fox jumped over the lazy fox.&#34;; String result = sentence.replaceAll&#40;&#34;fox&#34;, &#34;doggie&#34;&#41;; System.out.println&#40;&#34;Input: &#34; + sentence&#41;; System.out.println&#40;&#34;Output: &#34; + result&#41;; Would output: Input: The sly brown fox jumped over the lazy [...]]]></description>
			<content:encoded><![CDATA[<p>The replaceAll function in the java.lang.String class replaces each substring found in that matches the regular expression to replace.</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;"><span style="color: #003399;">String</span> sentence <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;The sly brown fox jumped over the lazy fox.&quot;</span><span style="color: #339933;">;</span>
<span style="color: #003399;">String</span> result <span style="color: #339933;">=</span> sentence.<span style="color: #006633;">replaceAll</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;fox&quot;</span>, <span style="color: #0000ff;">&quot;doggie&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #003399;">System</span>.<span style="color: #006633;">out</span>.<span style="color: #006633;">println</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Input: &quot;</span> <span style="color: #339933;">+</span> sentence<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #003399;">System</span>.<span style="color: #006633;">out</span>.<span style="color: #006633;">println</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Output: &quot;</span> <span style="color: #339933;">+</span> result<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>Would output:</p>
<p><code>Input: The sly brown fox jumped over the lazy fox.<br />
Output: The sly brown doggie jumped over the lazy doggie.</code></p>
<p>However there are cases where we want to replaceall substrings and ignore the case, or make it case insensitive.</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;"><span style="color: #003399;">String</span> sentence <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;The sly brown Fox jumped over the lazy foX.&quot;</span><span style="color: #339933;">;</span>
<span style="color: #003399;">String</span> result <span style="color: #339933;">=</span> sentence.<span style="color: #006633;">replaceAll</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;fox&quot;</span>, <span style="color: #0000ff;">&quot;dog&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #003399;">System</span>.<span style="color: #006633;">out</span>.<span style="color: #006633;">println</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Input: &quot;</span> <span style="color: #339933;">+</span> sentence<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #003399;">System</span>.<span style="color: #006633;">out</span>.<span style="color: #006633;">println</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Output: &quot;</span> <span style="color: #339933;">+</span> result<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p><code>Input: The sly brown Fox jumped over the lazy foX.<br />
Output: The sly brown Fox jumped over the lazy foX.</code></p>
<p>To create the case sensitive version of replaceAll we do not need to create a new wrapper function or create a utility class somewhere. All we need to do is prepend the Case-insensitve pattern modifier (?i) before our regex to indicate that we don't care about the case sensitivity of the regex.</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;"><span style="color: #003399;">String</span> sentence <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;The sly brown Fox jumped over the lazy foX.&quot;</span><span style="color: #339933;">;</span>
<span style="color: #003399;">String</span> result <span style="color: #339933;">=</span> sentence.<span style="color: #006633;">replaceAll</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;(?i)fox&quot;</span>, <span style="color: #0000ff;">&quot;dog&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #003399;">System</span>.<span style="color: #006633;">out</span>.<span style="color: #006633;">println</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Input: &quot;</span> <span style="color: #339933;">+</span> sentence<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #003399;">System</span>.<span style="color: #006633;">out</span>.<span style="color: #006633;">println</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Output: &quot;</span> <span style="color: #339933;">+</span> result<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p><code>Input: The sly brown Fox jumped over the lazy foX.<br />
Output: The sly brown dog jumped over the lazy dog.</code></p>
]]></content:encoded>
			<wfw:commentRss>http://jelaniharris.com/2009/case-insensitive-replaceall-in-java/feed/</wfw:commentRss>
		<slash:comments>9</slash:comments>
		</item>
	</channel>
</rss>

