I am busy rebuilding php files after our translators have been at them. And I am trying to replace strings inside a file with new values.
Here is the method that is giving me trouble
I'm getting this error
java.lang.IllegalArgumentException: Illegal group reference at java.util.regex.Matcher.appendReplacement(Matcher.java:554) at java.util.regex.Matcher.replaceAll(Matcher.java:646) at java.lang.String.replaceAll(String.java:1710) at lianolink.translation.data.website.PHPFileGenerator.restoreDefines(PHPFileGenerator.java:93)
This is the System.out.println that happens before the replace (this example works) \#\*EMAIL_PASSWORD_REMINDER_SUBJECT\*\# : define('EMAIL_PASSWORD_REMINDER_SUBJECT', STORE_NAME . ' - Nouveau mot de passe');
(This following example doesn't work) \#\*EMAIL_PASSWORD_REMINDER_BODY\*\# : define('EMAIL_PASSWORD_REMINDER_BODY', 'Un ... depuis ' . $REMOTE_ADDR . '.' . "\n\n" . 'Votre nouveau mot de passe pour \'' . STORE_NAME . '\' est :' . "\n\n" . ' %s' . "\n\n");
So can anyone tell me why this example doesn't work?
$ is a special character in Regular Expressions - its an anchor which matches at the end of the line. This could be giving you your exception - since rather than looking for the String "$REMOTE_ADDR" to replace, your expression will be looking for "REMOTE_ADDR" at the end of a line.
I sympathize with the task you've got BTW. PHP has never really supported internationalization. It casually uses 8 bits for characters for example, which means when you try to update the site for any Arabic or Hebrew readers, you've got problems.
Thank you for your sympathies! I agree, it makes no sense making this work when it's going to have to change when the arabic etc characters come along!
I guessed it was the $. But there is something I don't understand about your answer.
in the statement answer = answer.replaceAll(x,y);
The $ character you mentioned is in the y part of the statement, so java is not looking to match this part, it's looking for x to replace it with y. So why does the $ in y give a problem?
Hmm. You are of course right, and I'm guilty of reading questions too quickly. However, I can't generate the same error as you with a quick test. In fact I can all replaceAll against a string which is nothing but regular expression escape characters and not repeat this. So it looks like its something else.
[Rachel]: The $ character you mentioned is in the y part of the statement, so java is not looking to match this part, it's looking for x to replace it with y. So why does the $ in y give a problem?
Have you read the documentation for replaceAll()? The one in String refers to the one in Matcher, which says: "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."
So if your replacement string contains \ or $, you need to escape them with \\ and \$. Try something like:
I wouldn't have expected it to match either; however, a closes look at the java.lang.String)" target="_blank" rel="nofollow">Java Docs reveals the following:
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.
You can, of course, use \$ to get a literal dollar sign. [ November 17, 2004: Message edited by: Joel McNary ]
Piscis Babelis est parvus, flavus, et hiridicus, et est probabiliter insolitissima raritas in toto mundo.