2

After runing this

Names.replaceAll("^(\\w)\\w+", "$1.")

I have a String Like

Names = F.DA, ABC, EFG

I want a String format like

F.DA, A.BC & E.FG

How do I do that ?

Update :

If I had a name Like

Robert Filip, Robert Morris, Cirstian Jed

I want like

R.Filp, R.Morris & C.Jed

I will be happy, If also you suggest me a good resource on JAVA Regex.

4
  • 2
    Well, it works fine. Are you testing the regex on some other string?
    – Rohit Jain
    Commented Aug 6, 2013 at 10:07
  • So you want to replace the last comma with an &?
    – assylias
    Commented Aug 6, 2013 at 10:07
  • For me It's not working. Commented Aug 6, 2013 at 10:07
  • @assylias yes, I want to replace the , Commented Aug 6, 2013 at 10:08

3 Answers 3

6

You need to re-assign the result back to Names, since Strings are immutable, the replaceAll methods does not do in place replacement, rather it returns a new String:

names = names.replaceAll(", (?=[^,]*$)", " & ")
7
  • @user2579475. Now you need to show us the full code, as this is working on my Computer. There is no reason it can't work on your given String. There is something wrong somewhere else.
    – Rohit Jain
    Commented Aug 6, 2013 at 10:14
  • getName.replaceAll("^(\\w)\\w+", "$1.").replaceAll(", (?=[^,]*$)", " & ")); Commented Aug 6, 2013 at 10:26
  • @user2579475. Umm. Can you show your original String. It seems like your 1st replaceAll is the culprit. It doesn't give you the String which you have shown in the question. And please add the code to the question.
    – Rohit Jain
    Commented Aug 6, 2013 at 10:28
  • @user2579475. Print the output of getName.replaceAll("^(\\w)\\w+", "$1.") to see it gives the same string as in your question. I bet it doesn't.
    – Rohit Jain
    Commented Aug 6, 2013 at 10:29
  • @user2579475. See, your first replaceAll doesn't give what you thought it gave. Change it to: getName.replaceAll("^(\\w)(\\w+)", "$1.$2")
    – Rohit Jain
    Commented Aug 6, 2013 at 10:35
2

Following should work for you:

 String names = "Robert Filip, Robert Morris, Cirstian Jed, S.Smith";
 String repl  = names.replaceAll("((?:^|[^A-Z.])[A-Z])[a-z]*\\s(?=[A-Z])", "$1.")
                     .replaceAll(", (?=[^,]*$)", " & ");

 System.out.println(repl); //=> R.Filip, R.Morris, C.Jed & S.Smith

Explanation:

  1. 1st replaceAll call is matching a non-word && non-dot character + a capital letter in group #1 + 0 or more lower case letters + a space which should be followed by 1 capital letter. It is then inserting a dot in front of the match $1.
  2. 2ns replaceAll call is matching a comma that is not followed by another comma and replacing that by literal string " & ".
3
  • But, If I had a name Like Robert Filip, Robert Morris, Cirstian Jed. I want like R.Filp, R.Morris & C.Jed ?? Commented Aug 6, 2013 at 10:48
  • You solution wroked.But Can you answer my question. Commented Aug 6, 2013 at 10:49
  • @user2579475: Its better to provide realistic inputs in the question. Anyway I will edit my answer to suite your example names.
    – anubhava
    Commented Aug 6, 2013 at 10:51
0

Try this

    String names = "Amal.PM , Rakesh.KR , Ajith.N";
    names = names.replaceAll(" , (?=[^,]*$)", " & ");
    System.out.println("New String : "+names);

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.