<?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>info.michael-simons.eu &#187; IllegalArgumentException</title>
	<atom:link href="http://info.michael-simons.eu/tag/illegalargumentexception/feed/" rel="self" type="application/rss+xml" />
	<link>http://info.michael-simons.eu</link>
	<description>Just another nerd blog</description>
	<lastBuildDate>Wed, 25 Jan 2012 07:53:01 +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>Javas String.replaceAll</title>
		<link>http://info.michael-simons.eu/2009/07/21/javas-string-replaceall/</link>
		<comments>http://info.michael-simons.eu/2009/07/21/javas-string-replaceall/#comments</comments>
		<pubDate>Tue, 21 Jul 2009 09:35:56 +0000</pubDate>
		<dc:creator>Michael</dc:creator>
				<category><![CDATA[English posts]]></category>
		<category><![CDATA[Java]]></category>
		<category><![CDATA[IllegalArgumentException]]></category>
		<category><![CDATA[Matcher]]></category>
		<category><![CDATA[Pattern]]></category>
		<category><![CDATA[String]]></category>

		<guid isPermaLink="false">http://info.michael-simons.eu/?p=344</guid>
		<description><![CDATA[The following statement &#34;foo baz&#34;.replaceAll&#40;&#34;baz&#34;,&#34;$bar&#34;&#41;; will present you an java.lang.IllegalArgumentException: Illegal group reference exception as the replacement string can contain backreferences to the search pattern as stated in Mather#replaceAll: This method first resets this matcher. It then scans the input sequence looking for matches of the pattern. Characters that are not part of any match [...]]]></description>
			<content:encoded><![CDATA[<p>The following statement</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;"><span style="color: #0000ff;">&quot;foo baz&quot;</span>.<span style="color: #006633;">replaceAll</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;baz&quot;</span>,<span style="color: #0000ff;">&quot;$bar&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>will present you an <em>java.lang.IllegalArgumentException: Illegal group reference</em> exception as the replacement string can contain backreferences to the search pattern as stated in <a href="http://java.sun.com/j2se/1.5.0/docs/api/java/util/regex/Matcher.html#replaceAll(java.lang.String)">Mather#replaceAll</a>:</p>
<blockquote>
<p>
This method first resets this matcher. It then scans the input sequence looking for matches of the pattern. Characters that are not part of any match are appended directly to the result string; each match is replaced in the result by the replacement string. The replacement string may contain references to captured subsequences as in the appendReplacement method.<br />
<br />
Note that backslashes (\) and dollar signs ($) in the replacement string may cause the results to be different than if it were being treated as a literal replacement string. Dollar signs may be treated as references to captured subsequences as described above, and backslashes are used to escape literal characters in the replacement string.
</p>
</blockquote>
<p>Luckily, there is an easy solution:</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;"><span style="color: #0000ff;">&quot;foo baz&quot;</span>.<span style="color: #006633;">replaceAll</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;baz&quot;</span>,Matcher.<span style="color: #006633;">quoteReplacement</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;$bar&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p class="akst_link"><a href="http://info.michael-simons.eu/?p=344&amp;akst_action=share-this"  title="E-mail this, post to del.icio.us, etc." id="akst_link_344" class="akst_share_link " rel="nofollow">Share This</a>
</p>]]></content:encoded>
			<wfw:commentRss>http://info.michael-simons.eu/2009/07/21/javas-string-replaceall/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

<!-- Dynamic Page Served (once) in 0.487 seconds -->

