tags:

views:

483

answers:

7

I'm looking for the best approach for string find and replace in Java.

This is a sentence: "My name is Milan, people know me as Milan Vasic".

I want to replace the string Milan with Milan Vasic, but on place where I have already Milan Vasic, that shouldn't be a case.

after search/replace result should be: "My name is Milan Vasic, people know me as Milan Vasic".

I was try to use indexOf() and also Pattern/Matcher combination, but neither of my results not looks elegant, does someone have elegant solution?

cheers!

+11  A: 

Well, you can use a regular expression to find the cases where "Milan" isn't followed by "Vasic":

Milan(?! Vasic)

and replace that by the full name:

String.replaceAll("Milan(?! Vasic)", "Milan Vasic")

The (?!...) part is a negative lookahead which ensures that whatever matches isn't followed by the part in parentheses. It doesn't consume any characters in the match itself.

Alternatively, you can simply insert (well, technically replacing a zero-width match) the last name after the first name, unless it's followed by the last name already. This looks similar, but uses a positive lookbehind as well:

(?<=Milan)(?! Vasic)

You can replace this by just " Vasic" (note the space at the start of the string):

String.replaceAll("(?<=Milan)(?! Vasic)", " Vasic")

You can try those things out here for example.

Joey
Use java.lang.String.replaceAll(String, String)
helios
Thanks; I didn't look it up yet :)
Joey
Thanks man, this looks very elegant :)
vaske
Well, could be more elegant but when you know regular expressions it isn't too hard to read.
Joey
exactly, I'm in learning process of regex :) what will be the vice versa solution for example if I have "Vasic" and want to replace with "Milan Vasic", I mean with leading word?
vaske
You'd change the lookbehind into a negative one: `(?<!Milan )` and the lookahead into a positive: `(?=Vasic)`.
Joey
+5  A: 

When you dont want to put your hand yon regular expression (may be you should) you could first replace all "Milan Vasic" string with "Milan".

And than replace all "Milan" Strings with "Milan Vasic".

Markus Lausberg
+3  A: 

One possibility, reducing the longer form before expanding all:

string.replaceAll("Milan Vasic", "Milan").replaceAll("Milan", "Milan Vasic")

Another way, treating Vasic as optional:

string.replaceAll("Milan( Vasic)?", "Milan Vasic")

Others have described solutions based on lookahead or alternation.

Fabian Steeg
nice... but regex already let you indicate non capturing groups: (?! Milan)
helios
That's a negative lookahead. Non-capturing groups would be `(?: ...)`
Joey
+6  A: 

Another option:

"My name is Milan, people know me as Milan Vasic"
    .replaceAll("Milan Vasic|Milan", "Milan Vasic"))
McDowell
Thanks man, this could be very handy as well as first response!
vaske
Much more elegant answer!
lsiu
A: 

Simply include the Apache Commons Lang JAR and use the org.apache.commons.lang.StringUtils class. You'll notice lots of methods for replacing Strings safely and efficiently.

You can view the StringUtils API at the previously linked website.

"Don't reinvent the wheel"

Alasdair
+1  A: 

Try this:

public static void main(String[] args) {
    String str = "My name is Milan, people know me as Milan Vasic.";

    Pattern p = Pattern.compile("(Milan)(?! Vasic)");
    Matcher m = p.matcher(str);

    StringBuffer sb = new StringBuffer();

    while(m.find()) {
        m.appendReplacement(sb, "Milan Vasic");
    }

    m.appendTail(sb);
    System.out.println(sb);
}
lsiu
+1  A: 

you can use pattern matcher as well, which will replace all in one shot.

Pattern keyPattern = Pattern.compile(key); Matcher matcher = keyPattern.matcher(str); String nerSrting = matcher.replaceAll(value);

GK