3

For example...

String s = "abcxyzabcxyz";
s.replaceAll("xyz", "---");

This would normally give me "abc---abc---". How would I get it to give me "---xyz---xyz" instead?

Ogen
  • 6,499
  • 7
  • 58
  • 124
  • Well, my first shot would be `s.replaceAll("abc", "---")`, but I guess that's not what you're after, right? – Thorsten Dittmar Dec 04 '14 at 13:10
  • 3
    How is it supposed to handle this: `String s = "abcwxyzabctuvwxyz";`, `s.replaceAll("xyz", "123");` – Eypros Dec 04 '14 at 13:10
  • It shouldn't be too difficult... [replaceAll](http://docs.oracle.com/javase/6/docs/api/java/lang/String.html#replaceAll) takes a regular expression as the first argument – AJPerez Dec 04 '14 at 13:12
  • @Eypros I guess what he wants is some kind of "replace everything but `xyz` with `---`" instead of "replace `xyz` with `---`". Most probably the question could be rephrased to: Please tell me how to use a regular expression replace accordingly. – Thorsten Dittmar Dec 04 '14 at 13:13
  • the question is incomplete – Adrian Nasui Dec 04 '14 at 13:20
  • @AdrianNasui I find it perfectly comprehensible... In calls to `replaceAll` he specifies *what to replace*. He's looking for a way to specify *what to keep*. It's as simple as that. – Thorsten Dittmar Dec 04 '14 at 13:22
  • Well, Throsten, it is comprehensible, but as you stated, you have to do some 'guessing' to start giving an answer. :). Just saying! – Adrian Nasui Dec 04 '14 at 13:23
  • @AdrianNasui Well, the more often I read it the more clear it becomes. If I could I'd edit above comment to not contain the "I guess" part :-D – Thorsten Dittmar Dec 04 '14 at 13:24
  • What's incomplete is the method of replacing the characters he does not want to keep. Here is how I understand the requirement: Every substring between "xyz", including edges, should be replaced with "---". – Adrian Nasui Dec 04 '14 at 13:25

2 Answers2

5

String#replaceAll accepts a regex, if I understood you correctly, you can use:

s.replaceAll("[^xyz]", "---")

Explanation:

[^xyz] means any character that's not one of "x", "y" or "z".

In order to negate "xyz" as a series, use:

^(?!.*xyz).*$
Maroun
  • 94,125
  • 30
  • 188
  • 241
  • 1
    What if you have "xabcxyzaxbcxyz" it returns x---xyz-x--xyz. The regex should include the three letters in the same order and not any single appearance of them. I think [this](http://stackoverflow.com/a/1240293/1656663) should be used to build the regex – jesantana Dec 04 '14 at 13:32
  • 1
    That second regex `^(?!.*xyz).*$` replaces either the whole string or none of it. I think you want to use the accepted answer from @jesantana 's link: http://stackoverflow.com/a/1240337/1969638 – Zantier Dec 04 '14 at 13:52
1

You can use this...

s.replaceAll("[^xyz]", "-");

On a more formal Java approach, I would use the Pattern class, like this:

Pattern p = Pattern.compile("[^xyz]");
s.replaceAll(p.pattern(), "-");
mutlei
  • 145
  • 1
  • 8
  • As Adrian Nasui said in a nother comment, this yields `abc---------abc---------`. – Thorsten Dittmar Dec 04 '14 at 13:16
  • 1
    This has a problem in case the string is "xabcxyzaxbcxyz" returns x---xyz-x--xyz. The regex should include the three letters in the same order and not any single appearance of them – jesantana Dec 04 '14 at 13:22
  • This would be the best approach as it would replace letters on a singular basis instead of as a group as others are assuming. –  Dec 04 '14 at 13:23