• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Rob Spoor
  • Tim Cooke
  • Junilu Lacar
Sheriffs:
  • Henry Wong
  • Liutauras Vilda
  • Jeanne Boyarsky
Saloon Keepers:
  • Jesse Silverman
  • Tim Holloway
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Mikalai Zaikin
  • Piet Souris

Please help with regular expressions, $ and illegal groups

 
Ranch Hand
Posts: 434
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Bartender
Posts: 10336
Hibernate Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
$ 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.
 
Rachel Swailes
Ranch Hand
Posts: 434
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 434
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 10336
Hibernate Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Wanderer
Posts: 18671
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
[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:
 
Bartender
Posts: 1843
Eclipse IDE Ruby Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I wouldn't have expected it to match either; however, a closes look at the 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 ]
 
Joel McNary
Bartender
Posts: 1843
Eclipse IDE Ruby Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I've been Yingsted!
 
Jim Yingst
Wanderer
Posts: 18671
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


 
Ranch Hand
Posts: 262
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If you're using jdk 1.5, you can preprocess the replacement string with the new quoteReplacement method:Otherwise, you can roll your own:
 
You showed up just in time for the waffles! And this tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic