{"id":773,"date":"2006-02-09T15:04:36","date_gmt":"2006-02-09T19:04:36","guid":{"rendered":"http:\/\/wordpress.cephas.net\/?p=773"},"modified":"2006-02-09T15:04:36","modified_gmt":"2006-02-09T19:04:36","slug":"javalangillegalargumentexception-illegal-group-reference-replaceall-and-dollar-signs","status":"publish","type":"post","link":"https:\/\/cephas.net\/blog\/2006\/02\/09\/javalangillegalargumentexception-illegal-group-reference-replaceall-and-dollar-signs\/","title":{"rendered":"java.lang.IllegalArgumentException: Illegal group reference, replaceAll and dollar signs"},"content":{"rendered":"<p>This weblog is officially about inane things I run into while trying to do my job at work.  Let&#8217;s say you have a String object like this:<br \/>\n<code><br \/>\nString mystring = \"Your password: #PASSWORD\";<br \/>\n<\/code><br \/>\nand at runtime you need to replace the value of <code>#PASSWORD<\/code> with a password that a user typed in. You&#8217;d write something like this:<br \/>\n<code><br \/>\nString password = \"$Jslwe\"<br \/>\nmystring = mystring.replaceAll(\"#PASSWORD\", password);<br \/>\n<\/code><br \/>\nWhat would happen? You&#8217;d expect that the key <code>#PASSWORD<\/code> would get replaced with the value of the variable &#8216;password&#8217; (which is &#8220;$Jslwe&#8221;) and then you&#8217;d move happily on your way to something much more interesting. But no, Java throws you an error:<br \/>\n<code><br \/>\njava.lang.IllegalArgumentException: Illegal group reference<br \/>\n<\/code><br \/>\nwhich is extremely helpful. Turns out that the second argument to the <a href=\"http:\/\/java.sun.com\/j2se\/1.5.0\/docs\/api\/java\/lang\/String.html#replaceAll(java.lang.String,%20java.lang.String)\">String replaceAll<\/a> method &#8220;may&#8221; have some issues with dollar signs and backslashes which you only find out about if you dig into the <a href=\"http:\/\/java.sun.com\/j2se\/1.5.0\/docs\/api\/java\/util\/regex\/Matcher.html#replaceAll(java.lang.String)\">Matcher <\/a> class that backs the replaceAll method or if you&#8217;re lucky and you <a href=\"http:\/\/www.regular-expressions.info\/java.html\">read about the whole thing<\/a> on a site devoted to regular expressions. In short:<\/p>\n<blockquote><p>\nmyString.replaceAll(&#8220;regex&#8221;, &#8220;replacement&#8221;) replaces all regex matches inside the string with the replacement string you specified. No surprises here. All parts of the string that match the regex are replaced. You can use the contents of capturing parentheses in the replacement text via $1, $2, $3, etc. $0 (dollar zero) inserts the entire regex match. $12 is replaced with the 12th backreference if it exists, or with the 1st backreference followed by the literal &#8220;2&#8221; if there are less than 12 backreferences. If there are 12 or more backreferences, it is not possible to insert the first backreference immediately followed by the literal &#8220;2&#8221; in the replacement text.<\/p>\n<p>In the replacement text, a dollar sign not followed by a digit causes an IllegalArgumentException to be thrown. If there are less than 9 backreferences, a dollar sign followed by a digit greater than the number of backreferences throws an IndexOutOfBoundsException. So be careful if the replacement string is a user-specified string. To insert a dollar sign as literal text, use \\$ in the replacement text. When coding the replacement text as a literal string in your source code, remember that the backslash itself must be escaped too: &#8220;\\\\$&#8221;.\n<\/p><\/blockquote>\n","protected":false},"excerpt":{"rendered":"<p>This weblog is officially about inane things I run into while trying to do my job at work. Let&#8217;s say you have a String object like this: String mystring = &#8220;Your password: #PASSWORD&#8221;; and at runtime you need to replace the value of #PASSWORD with a password that a user typed in. You&#8217;d write something &hellip; <a href=\"https:\/\/cephas.net\/blog\/2006\/02\/09\/javalangillegalargumentexception-illegal-group-reference-replaceall-and-dollar-signs\/\" class=\"more-link\">Continue reading <span class=\"screen-reader-text\">java.lang.IllegalArgumentException: Illegal group reference, replaceAll and dollar signs<\/span> <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":[],"categories":[3,2],"tags":[],"_links":{"self":[{"href":"https:\/\/cephas.net\/blog\/wp-json\/wp\/v2\/posts\/773"}],"collection":[{"href":"https:\/\/cephas.net\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/cephas.net\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/cephas.net\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/cephas.net\/blog\/wp-json\/wp\/v2\/comments?post=773"}],"version-history":[{"count":0,"href":"https:\/\/cephas.net\/blog\/wp-json\/wp\/v2\/posts\/773\/revisions"}],"wp:attachment":[{"href":"https:\/\/cephas.net\/blog\/wp-json\/wp\/v2\/media?parent=773"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/cephas.net\/blog\/wp-json\/wp\/v2\/categories?post=773"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/cephas.net\/blog\/wp-json\/wp\/v2\/tags?post=773"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}