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
• Liutauras Vilda
• Tim Cooke
• Jeanne Boyarsky
• Paul Clapham
Sheriffs:
• Devaka Cooray
• Ron McLeod
• paul wheaton
Saloon Keepers:
• Tim Moores
• Piet Souris
• Tim Holloway
• Stephan van Hulst
• Carey Brown
Bartenders:
• Al Hobbs
• Frits Walraven
• Scott Selikoff

replacing string with '\' character

Ranch Hand
Posts: 106
• Number of slices to send:
Optional 'thank-you' note:
Hi,
I'm trying to do a string.replaceAll(MatcherGroupThatContainsFilePath, "new string");

I'm getting an error.

compling with 1.5

I'm reading a file, looking for a regex, then creating a new string that modifies the text the regex found, then trying to replace the old string with the new. It's the first argument in the text.replaceAll(old, new) that's the problem.
thanks,
jason

Ranch Hand
Posts: 1183
• Number of slices to send:
Optional 'thank-you' note:
Could you post what MatcherGroupThatContainsFilePath is holding ?

Edmund Castermund
Ranch Hand
Posts: 106
• Number of slices to send:
Optional 'thank-you' note:
Hi,
the string I'm trying to replace has a windows file path in it. Here's some code that recreates the problem:

this generates:

In perl there's a function: \Q$string\E you can use, where you're telling perl that you don't want anything inside$string to be evaluated as a regex. Is there anything similar in java?
What I'm trying to do is to in the actual situation (it's for work, so I don't want to post the actual data) is replace a <!DOCTYPE root-element c:\path\to\dtd> tag (that contains a file path) with a commented out doctype tag: <!-- <!DOCTYPE root-element c:\path\to\dtd> -->
jason

Marshal
Posts: 76822
366
• Number of slices to send:
Optional 'thank-you' note:
Try using \\\\ instead of \\.

What happens is that the compiler sees \\ as an escape for \, so it looks for something after the \\ and finds \h which isn't appropriate. You want it to see \\ so you need to escape the first \ to \\ and you need to escape the second \ to \\.

Edmund Castermund
Ranch Hand
Posts: 106
• Number of slices to send:
Optional 'thank-you' note:
Hi,
I tried that, and quite a few variations besides...but I still couldn't get it to work.
The code you mentioned just didn't do anything.

I found this code online somewhere that seemed to do the trick:

Edmund Castermund
Ranch Hand
Posts: 106
• Number of slices to send:
Optional 'thank-you' note:
check that...your code did work...

This gave me the desired result:

I think the reason it wasn't working for me before is that I didn't create a new variable...
thanks

Campbell Ritchie
Marshal
Posts: 76822
366
• Number of slices to send:
Optional 'thank-you' note:
You're welcome

 I'm so happy! And I wish to make this tiny ad happy too: the value of filler advertising in 2021 https://coderanch.com/t/730886/filler-advertising