Skip to Main Content

Java Programming

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Using special chars in RegEx causes problems

807605Oct 1 2007 — edited Oct 2 2007
Hello Folks,

I have a single string of numbers positive and negative separated by a delimiter. I am trying to convert the negative numbers which are represented by brackets into ones without brakcets e.g. (1.235) to -1.235

Here is what I have in my code
arCols[j].replaceAll("\(", "-");
arCols[j].replaceAll("\)", "");

But that gives a compilation problem as below.

Exception in thread "main" java.lang.Error: Unresolved compilation problem:
Invalid escape sequence (valid ones are \b \t \n \f \r \" \' \\ )

And if I remove the leading slash that tries to escape the character as below

arCols[j].replaceAll("(", "-");
arCols[j].replaceAll(")", "");

I get the following error.

Exception in thread "main" java.util.regex.PatternSyntaxException: Unclosed group near index 1
(

How can I replace the brackets using regular expression search and replace?

Thanks for your help.

Sanjay.
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Oct 30 2007
Added on Oct 1 2007
5 comments
379 views