wood burning stoves 2.0*
The moose likes Java in General and the fly likes Please help with regular expressions, $ and illegal groups Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Please help with regular expressions, $ and illegal groups" Watch "Please help with regular expressions, $ and illegal groups" New topic
Author

Please help with regular expressions, $ and illegal groups

Rachel Swailes
Ranch Hand

Joined: May 18, 2004
Posts: 434
Hi there

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?

Many kind regards,
Rachel
Paul Sturrock
Bartender

Joined: Apr 14, 2004
Posts: 10336

$ 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.


JavaRanch FAQ HowToAskQuestionsOnJavaRanch
Rachel Swailes
Ranch Hand

Joined: May 18, 2004
Posts: 434
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?

Thanks!
Rachel
[ November 17, 2004: Message edited by: Rachel Swailes ]
Rachel Swailes
Ranch Hand

Joined: May 18, 2004
Posts: 434
With deadlines looming I went for the oldfashioned-nonregex-looping-wildly-into-the-night-approach.

I am still very interested to hear the answer to this as I am sure I will see this nasty thing again.

Many kind regards,
Rachel
Paul Sturrock
Bartender

Joined: Apr 14, 2004
Posts: 10336

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.
Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18671
[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'm not back." - Bill Harding, Twister
Joel McNary
Bartender

Joined: Aug 20, 2001
Posts: 1817

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.
Joel McNary
Bartender

Joined: Aug 20, 2001
Posts: 1817

I've been Yingsted!
Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18671


Alan Moore
Ranch Hand

Joined: May 06, 2004
Posts: 262
If you're using jdk 1.5, you can preprocess the replacement string with the new quoteReplacement method:Otherwise, you can roll your own:
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Please help with regular expressions, $ and illegal groups