• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

ReplaceAll Using a Variable is Throwing Error

 
Ranch Hand
Posts: 2206
2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The following code throws the java.lang.IllegalArgumentException: Illegal group reference.

If I manually type in the replacement variable it works great.



This is the string that is generated by the new QMSConfiguration call
SystemOut     O PathAdjuster: //sgai-fs02/qmsattachments\\$/QMSAttachments/
 
Sheriff
Posts: 67746
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
That call makes no sense. The first parameter is expected to be a regex.
 
Bartender
Posts: 1845
10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
How about if you store the value in a local String?

i.e.

 
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

The second parameter of replaceAll() is the replacement string. And with a replacement string, the $ has special meaning. It is expected to be followed by a number, that represents the group that was captured, when the regex (first parameter) was used to find the substring to replace. In your case, the $ is not followed by a number.

Henry
 
Stefan Evans
Bartender
Posts: 1845
10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
But the $ is preceded by a backslash to escape it...(at least it is in the example he gave)

(*lightbulb*) or it's MEANT to be.

In the original post, Steve posted it with TWO backslashes.  Which is exactly the value you would want to represent it as a STRING, but not the value you want stored in the configuration.
i.e. one backslash is required to escape the $ in the regex replacement value : \$
If you put this replacement value into a String, you need to escape your backslash:  hence "\\$"


To demonstrate:


Which results in:
//sgai-fs02/qmsattachments\$/QMSAttachments/
//sgai-fs02/qmsattachments$/QMSAttachments/ //sgai-fs02/qmsattachments$/QMSAttachments/

Note how the "newValue" printed only includes one backslash?
@Steve:  Remove one of those backslash characters before your $ in the configuration value and it should be good.
 
Henry Wong
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

The extra backslash is only needed if the string is represented as a string literal -- for the compiler to parse. For the actual string object itself (or if you try to print it), there is no extra backslash.

Anyway, if you need to remove all the special meaning off of a replacement string, then I recommend that you use the Matcher.quoteReplacement() method.

Henry
 
There is no greater crime than stealing somebody's best friend. I miss you tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic