This week's book giveaway is in the OO, Patterns, UML and Refactoring forum. We're giving away four copies of Refactoring for Software Design Smells: Managing Technical Debt and have Girish Suryanarayana, Ganesh Samarthyam & Tushar Sharma on-line! See this thread for details.
This one compiles, but then gives me a dangling meta character error when I run it.
Trying to replace another character, such as Y, works fine doing this:
String oldStr = "XYZ*";
String newStr = oldStr.replaceAll("Y", "[A-Za-z0-9]*");
System.out.println("newStr=" + newStr + "\n");
That compiles and runs and works. So my basic syntax (for replacing non meta characters) is correct here.
So how do I take a string, and replace every occurrence of * with A-Za-z0-9]*
If I do not escape it, it is read as a meta character.
If I do escape it, it tells me it is an illegal use of an escape character.
Joined: May 04, 2009
I found the answer in another forum,
So in case anyone else stumbles on this doing a search, the answer is to use double escape characters.
The reason is that the String literal turns the \\ into a single \ before it analyses the regular expression so you get a proper \*. You will occasionally have to use \\\\ in regular expressions, or even \\\\\\\\
I’ve looked at a lot of different solutions, and in my humble opinion Aspose is the way to go. Here’s the link: http://aspose.com
subject: How to replace character *, using .replaceAll