* BALE-6626
Check If a String Contains Non-Alphanumeric Characters in Java

* Move patterns to class level.

---------

Co-authored-by: parthiv39731 <parthiv39731@gmail.com>
This commit is contained in:
parthiv39731
2023-07-04 05:17:45 -07:00
committed by GitHub
parent 50e477a72a
commit 9bd2cdbc5e
4 changed files with 375 additions and 0 deletions

View File

@@ -0,0 +1,50 @@
package com.baeldung.nonalphanumeric;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class NonAlphaNumRegexChecker {
private static final Pattern PATTERN_NON_ALPHNUM_ANYLANG = Pattern.compile("[^\\p{IsAlphabetic}\\p{IsDigit}]");
private static final Pattern PATTERN_NON_ALPHNUM_USASCII = Pattern.compile("[^a-zA-Z0-9]+");
/**
* checks if a non-alphanumeric character is present. this method would return true if
* it comes across a non english or non US-ASCII letter
*
* @param str - String to check for special character
* @return true if special character found else false
*/
public static boolean isNonAlphanumeric(String str) {
// Pattern pattern = Pattern.compile("\\W"); //same as [^a-zA-Z0-9]+
// Pattern pattern = Pattern.compile("[^a-zA-Z0-9||\\s]+"); //ignores space
Matcher matcher = PATTERN_NON_ALPHNUM_USASCII.matcher(str);
return matcher.find();
}
/**
* Checks for non-alphanumeric characters from all language scripts
*
* @param input - String to check for special character
* @return true if special character found else false
*/
public static boolean containsNonAlphanumeric(String input) {
// Pattern pattern = Pattern.compile("[^\\p{Alnum}]", Pattern.UNICODE_CHARACTER_CLASS); //Binary properties
Matcher matcher = PATTERN_NON_ALPHNUM_ANYLANG.matcher(input);
return matcher.find();
}
/**
* checks for non-alphanumeric character. it returns true if it detects any character other than the
* specified script argument. example of script - Character.UnicodeScript.GEORGIAN.name()
*
* @param input - String to check for special character
* @param script - language script
* @return true if special character found else false
*/
public static boolean containsNonAlphanumeric(String input, String script) {
String regexScriptClass = "\\p{" + "Is" + script + "}";
Pattern pattern = Pattern.compile("[^" + regexScriptClass + "\\p{IsDigit}]"); //Binary properties
Matcher matcher = pattern.matcher(input);
return matcher.find();
}
}

View File

@@ -0,0 +1,49 @@
package com.baeldung.nonalphanumeric;
import org.apache.commons.lang3.StringUtils;
public class NonAlphaNumericChecker {
/**
* Checks for non-alphanumeric characters in any Unicode Script
* @param str - String to check for special characters
* @return true if special character found else false
*/
public static boolean isNonAlphanumericAnyLangScript(String str) {
for (int i = 0; i < str.length(); i++) {
char c = str.charAt(i);
if (!Character.isLetterOrDigit(c)) {
return true;
}
}
return false;
}
/**
* checks for special characters,returns false if any character
* found other than the script argument
* @param str - String to check for special characters
* @param script - Language script
* @return true if special character found else false
*/
public static boolean isNonAlphanumericInLangScript(String str, String script) {
for (int i = 0; i < str.length(); i++) {
char c = str.charAt(i);
// script e.g., Character.UnicodeScript.of(c).toString().equalsIgnoreCase(Character.UnicodeScript.LATIN.toString())
if (!Character.UnicodeScript.of(c).toString().equalsIgnoreCase(script)
&& !Character.isDigit(c)) {
return true;
}
}
return false;
}
/**
* checks for special characters in any lang
* @param str - String to check for special characters
* @return true if special character found else false
*/
public static boolean isNonAlphanumericAnyLangScriptV2(String str) {
return !StringUtils.isAlphanumeric(str);
}
}