2

How to replace every single characters, till the end of a line, using Java regex?

For Example: I want to replace every character with XXX right after = sign

contact.name=Stack Overflow

contact.name=XXX

3 Answers 3

3

Try this:

String example = "contact.name=Stack Overflow";
example = example.replaceAll("=.*", "=XXX");
Sign up to request clarification or add additional context in comments.

4 Comments

.replaceAll("=.*", "=XXX"); would be sufficient. The braces are redundant.
Ah this worked perfectly. Just what I looked for. Thank you for both of you, Oscar and Mana.
@azeemigi Please consider accepting the answer that was most helpful for you :) (mark the check to its left)
@Oscar I checked this answer :)
1

This should take care of it

Matcher matcher = Pattern.compile("=(.*?)\n").matcher(string);
if (matcher.find()) {
    string = string.substring(0, string.length - matcher.group(1).length);
    for (int i = 0; i < matcher.group(1).length; i ++) {
        string += "X";
    }
}

7 Comments

Yikes, why not use replaceAll?
Sorry, added too much complication. I thought we were after replacing each character with exactly one X.
Hmm, I can understand why you thought that, the original question isn't quite clear.
No problem :) I'm glad you've found your answer
I actually was looking for how to replace each character with exactly one X, and this was the top search result and helped me. I was hoping there was some way to do it with replaceAll, but I guess not.
|
0
String ex = "contact.name=Stack Overflow";
ex = ex.replaceAll("=\w+(.*)", "=XXX");

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.