0

I have a String in which I am trying to replace the number enclosed by two backslashes. For example: \10\ , I am trying to replace that with 10. I am currently using this regex to do that:

String texter = texthb.replaceAll("\\.+\\", "\\"+String.valueOf(pertotal + initper)+"\\");

This line is giving the following error:

 Exception in thread "AWT-EventQueue-0" java.util.regex.PatternSyntaxException: Unexpected internal error near index 4

.+\

I know it is because the regex is wrong. What is the proper way to accomplish this? Thanks in advance.

2
  • 1
    if you just need to remove all back slashes, you can simply write texter = textbh.replace("\\", ""); Commented Jun 17, 2015 at 17:45
  • 1
    In code: "\\\\.+\\\\" In variable: \\.+\\ Commented Jun 17, 2015 at 17:46

1 Answer 1

2

Use four backslashes to match a single backslash character.

String texter = texthb.replaceAll("\\\\.+?\\\\", "\\\\"+String.valueOf(pertotal + initper)+"\\\\");
Sign up to request clarification or add additional context in comments.

2 Comments

With that I get the error: java.lang.IllegalArgumentException: character to be escaped is missing
That works thanks so much. I will mark you as correct when it allows me.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.