Replacing all non-alphanumeric characters with empty strings

JavaRegexNon Alphanumeric

Java Problem Overview


I tried using this but didn't work-

return value.replaceAll("/[^A-Za-z0-9 ]/", "");

Java Solutions


Solution 1 - Java

Use [^A-Za-z0-9].

Note: removed the space since that is not typically considered alphanumeric.

Solution 2 - Java

Try

return value.replaceAll("[^A-Za-z0-9]", "");

or

return value.replaceAll("[\\W]|_", "");

Solution 3 - Java

You should be aware that [^a-zA-Z] will replace characters not being itself in the character range A-Z/a-z. That means special characters like é, ß etc. or cyrillic characters and such will be removed.

If the replacement of these characters is not wanted use pre-defined character classes instead:

 str.replaceAll("[^\\p{IsAlphabetic}\\p{IsDigit}]", "");

PS: \p{Alnum} does not achieve this effect, it acts the same as [A-Za-z0-9].

Solution 4 - Java

return value.replaceAll("[^A-Za-z0-9 ]", "");

This will leave spaces intact. I assume that's what you want. Otherwise, remove the space from the regex.

Solution 5 - Java

You could also try this simpler regex:

 str = str.replaceAll("\\P{Alnum}", "");

Solution 6 - Java

Java's regular expressions don't require you to put a forward-slash (/) or any other delimiter around the regex, as opposed to other languages like Perl, for example.

Solution 7 - Java

Solution:

value.replaceAll("[^A-Za-z0-9]", "")

Explanation:

>[^abc] When a caret ^ appears as the first character inside square brackets, it negates the pattern. This pattern matches any character except a or b or c.

Looking at the keyword as two function:

  • [(Pattern)] = match(Pattern)
  • [^(Pattern)] = notMatch(Pattern)

Moreover regarding a pattern:

  • A-Z = all characters included from A to Z

  • a-z = all characters included from a to z

  • 0=9 = all characters included from 0 to 9

Therefore it will substitute all the char NOT included in the pattern

Solution 8 - Java

I made this method for creating filenames:

public static String safeChar(String input)
{
    char[] allowed = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ-_".toCharArray();
    char[] charArray = input.toString().toCharArray();
    StringBuilder result = new StringBuilder();
    for (char c : charArray)
    {
        for (char a : allowed)
        {
            if(c==a) result.append(a);
        }
    }
    return result.toString();
}

Solution 9 - Java

If you want to also allow alphanumeric characters which don't belong to the ascii characters set, like for instance german umlaut's, you can consider using the following solution:

 String value = "your value";

 // this could be placed as a static final constant, so the compiling is only done once
 Pattern pattern = Pattern.compile("[^\\w]", Pattern.UNICODE_CHARACTER_CLASS);
    
 value = pattern.matcher(value).replaceAll("");

Please note that the usage of the UNICODE_CHARACTER_CLASS flag could have an impose on performance penalty (see javadoc of this flag)

Solution 10 - Java

Using Guava you can easily combine different type of criteria. For your specific solution you can use:

value = CharMatcher.inRange('0', '9')
        .or(CharMatcher.inRange('a', 'z')
        .or(CharMatcher.inRange('A', 'Z'))).retainFrom(value)

Solution 11 - Java

Simple method:

public boolean isBlank(String value) {
	return (value == null || value.equals("") || value.equals("null") || value.trim().equals(""));
}

public String normalizeOnlyLettersNumbers(String str) {
    if (!isBlank(str)) {
   		return str.replaceAll("[^\\p{L}\\p{Nd}]+", "");
    } else {
   		return "";
    }
}

Solution 12 - Java

public static void main(String[] args) {
	String value = " Chlamydia_spp. IgG, IgM & IgA Abs (8006) ";
	
	System.out.println(value.replaceAll("[^A-Za-z0-9]", ""));

}

output: ChlamydiasppIgGIgMIgAAbs8006

Github: https://github.com/AlbinViju/Learning/blob/master/StripNonAlphaNumericFromString.java

Solution 13 - Java

Guava's https://guava.dev/releases/19.0/api/docs/com/google/common/base/CharMatcher.html">CharMatcher</a> provides a concise solution:

output = CharMatcher.javaLetterOrDigit().retainFrom(input);

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionAlex GomesView Question on Stackoverflow
Solution 1 - JavaMirek PlutaView Answer on Stackoverflow
Solution 2 - JavaAndrew DuffyView Answer on Stackoverflow
Solution 3 - JavaAndre SteingressView Answer on Stackoverflow
Solution 4 - JavaericksonView Answer on Stackoverflow
Solution 5 - JavasauravView Answer on Stackoverflow
Solution 6 - JavaabyxView Answer on Stackoverflow
Solution 7 - JavaGalloCedroneView Answer on Stackoverflow
Solution 8 - JavazneoView Answer on Stackoverflow
Solution 9 - JavasnapView Answer on Stackoverflow
Solution 10 - JavaDebView Answer on Stackoverflow
Solution 11 - JavaAlberto CerqueiraView Answer on Stackoverflow
Solution 12 - JavaAlbinView Answer on Stackoverflow
Solution 13 - JavaBunarroView Answer on Stackoverflow