Skip to Main Content

Java Programming

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Replacing special chars in Text (like German Umlauts) in a 1 liner?

807607Nov 9 2006 — edited Nov 9 2006
Theme: Regular Expression

Hi developers,

today i had the challenge to replace German Umlauts in a given text (this solution fits to ANY special Character, not only German Umlauts...).
No problem at all (see code sample at end of post), but i tried to find the PERFECT/SHORTEST solution for this challenge , but it doesn' work...
This would be the following 1 line "replaceAll()" - Statement:

Hashtable "specialChars" filled like in the working solution at the end of this post...
...
inText = inText.replaceAll("[�������]", (String)specialChars.get("$0"));
But this won't work. Following error occurs:
Exception in thread "main" java.lang.NullPointerException
        at java.util.regex.Matcher.appendReplacement(Matcher.java:692)
        at java.util.regex.Matcher.replaceAll(Matcher.java:806)
        at java.lang.String.replaceAll(String.java:2000)
        at specialReplace.<init>(specialReplace.java:44)
        at specialReplace.main(specialReplace.java:51)
This error occurs, because there is no key "$0" in the Hash.
But i'm not looking for "$0" of course, i'm looking for the returning regex value in group 0...


MY QUESTION:

Isn't it possible at all, to get a result from an external function (or here: getting a value from a Hash)
by passing a regex value by reference (here: $0), or is just my replaceAll() line wrong?




Here's my working solution for the challenge (perhaps useful for someone...):
import java.util.Hashtable;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class specialReplace {

  //Constants
  private final Hashtable specialChars = fillSpecialChars();
  private final Hashtable fillSpecialChars() {
   	Hashtable hm = new Hashtable(7);
    	hm.put("�", "ae");
    	hm.put("�", "ue");
    	hm.put("�", "oe");
    	hm.put("�", "Ae");
    	hm.put("�", "Ue");
    	hm.put("�", "Oe");
    	hm.put("�", "ss");
    	//add any key/value pair here to expand the functionality of this class...
  	return hm;
  }
  
  //replaceSpecialChars
  private String replaceSpecialChars(String charsRegex, String inText) {
  	StringBuffer sb = new StringBuffer();
  	Pattern p = Pattern.compile(charsRegex, Pattern.CANON_EQ);
  	Matcher m = p.matcher(inText);
  	try {
    	while (m.find()) {
   		  m.appendReplacement(sb, (String)specialChars.get(m.group(0)));
    	}	
    	inText = m.appendTail(sb).toString();
    } catch (Exception e) {
       e.printStackTrace();
    } finally {
    	  sb = null;
    	  p = null;
    	  m = null;
    }
    return inText;
  }
  
  //constructor
  public specialReplace() {
  	//A senseless german text which includes the characters to be replaced...
  	String testStr = "Dies w�re keine �berraschung, wenn alle Umlaute ersetzt w�rden."
  	               + "\n�deme machen �rger, da� hei�t, keiner will sie haben (a�����)...";;
  	
  	//This is, what i would like to get to work. Would make the function replaceSpecialChars() needless...     
  	/*          
  	testStr = testStr.replaceAll("[�������]", (String)specialChars.get("$0"));               
  	System.out.println("\nResult:\n-------\n" + testStr);
  	*/
  	               
   	System.out.println("\nResult:\n-------\n" + replaceSpecialChars("[�������]", testStr)); 
  }
  
  //main
  public static void main(String[] args) {
    	new specialReplace();
  }
}
Any idea for a solution someone, to get the 1 liner to work?

TIA, aumaster
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Dec 7 2006
Added on Nov 9 2006
4 comments
2,818 views