1

I have input as below:

Input: 6jVYY3Xnqt<>:"/\|?*GjznpnRQSb
testInput = testInput.replaceAll("[<>:/\\\"|?*]", "-");
output: 6jVYY3Xnqt----\---GjznpnRQSb

But if I do:

testInput = testInput.replaceAll("[<>:/\"|?*]", "-");
testInput = testInput.replace("\\", "-");
output: 6jVYY3Xnqt--------GjznpnRQSb

Is this a bug in java 7? Why is replaceAll not taking the \ character?

1 Answer 1

2

You need to double escape the backslash in your regular expression, once for the string literal and once for the regular expression:

testInput= testInput.replaceAll("[<>:/\\\\\"|?*]", "-");
//                                    ^^^^
//                                    Represents one backslash
Sign up to request clarification or add additional context in comments.

Comments

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.