...">

Regex using Java String.replaceAll

I want to replace the value of a java string as follows. below code is not working.

cleanInst.replaceAll("[<i>]", ""); cleanInst.replaceAll("[</i>]", ""); cleanInst.replaceAll("[//]", "/"); cleanInst.replaceAll("[\bPhysics Dept.\b]", "Physics Department"); cleanInst.replaceAll("[\b/n\b]", ";"); cleanInst.replaceAll("[\bDEPT\b]", "The Department"); cleanInst.replaceAll("[\bDEPT.\b]", "The Department"); cleanInst.replaceAll("[\bThe Dept.\b]", "The Department"); cleanInst.replaceAll("[\bthe dept.\b]", "The Department"); cleanInst.replaceAll("[\bThe Dept\b]", "The Department"); cleanInst.replaceAll("[\bthe dept\b]", "The Department"); cleanInst.replaceAll("[\bDept.\b]", "The Department"); cleanInst.replaceAll("[\bdept.\b]", "The Department"); cleanInst.replaceAll("[\bdept\b]", "The Department"); 

What is the easiest way to achieve the above replacement?

+7
source share
3 answers

If this is a feature that you constantly use, a problem arises. Each regular expression is compiled for each call. It is best to create them as constants. You may have something like this.

 private static final Pattern[] patterns = { Pattern.compile("</?i>"), Pattern.compile("//"), // Others }; private static final String[] replacements = { "", "/", // Others }; public static String cleanString(String str) { for (int i = 0; i < patterns.length; i++) { str = patterns[i].matcher(str).replaceAll(replacements[i]); } return str; } 
+11
source
 cleanInst.replaceAll("[<i>]", ""); 

it should be:

 cleanInst = cleanInst.replaceAll("[<i>]", ""); 

since the String class is immutable and does not change its internal state, i.e. replaceAll() returns a new instance other than cleanInst .

+6
source

You should read the basic regular expression tutorial .

So far, what you tried to do can be done as follows:

 cleanInst = cleanInst.replace("//", "/"); cleanInst = cleanInst.replaceAll("</?i>", ""); cleanInst = cleanInst.replaceAll("/n\\b", ";") cleanInst = cleanInst.replaceAll("\\bPhysics Dept\\.", "Physics Department"); cleanInst = cleanInst.replaceAll("(?i)\\b(?:the )?dept\\b\\.?", "The Department"); 

Perhaps you can link all these replacement operations (but I don't know the correct Java syntax for this).

On word boundaries : \b usually makes sense immediately before or after an alphanumeric character.

For example, \b/n\b will match only /n if it is immediately preceded by an alphanumeric character followed by a non-alphanumeric character, so it matches "a/n!" but not "foo /n bar" .

+2
source

All Articles