How to Determine If a String Has Non-Alphanumeric Characters

How to determine if a String has non-alphanumeric characters?

Using Apache Commons Lang:

!StringUtils.isAlphanumeric(String)

Alternativly iterate over String's characters and check with:

!Character.isLetterOrDigit(char)

You've still one problem left:
Your example string "abcdefà" is alphanumeric, since à is a letter. But I think you want it to be considered non-alphanumeric, right?!

So you may want to use regular expression instead:

String s = "abcdefà";
Pattern p = Pattern.compile("[^a-zA-Z0-9]");
boolean hasSpecialChar = p.matcher(s).find();

Check if string contains only non-alphanumeric characters

If you just want to see if it contains an alphanumeric, then you can find() it

import java.util.regex.Matcher;
import java.util.regex.Pattern;

public String clean(String line) {
Pattern p = Pattern.compile("\w");

Matcher m = p.matcher(line);
if (m.find()) {
return line; // found any character or digit, keep the line
}
return ""; // else return nothing
}

Reject String If Contains Any Non-Alpha Numeric Character

Create a separate method to call the String you are searching through:

public boolean isAlphanumeric(String str)
{
char[] charArray = str.toCharArray();
for(char c:charArray)
{
if (!Character.isLetterOrDigit(c))
return false;
}
return true;
}

Then, add the following if statement to the above code prior to the second try statement.

if (isAlphanumeric(wordToSearch) == true)

How to find out if string contains non-alpha numeric characters in C#/.NET 2.0?

I don't know how special characters from all those languages are categorised, but you could check if the Char.IsLetterOrDigit method matches what you want to do. It works at least for the digits and letters I tested:

string test = "Aasdf345ÅÄÖåäöéÉóÓüÜïÏôÔ";
if (test.All(Char.IsLetterOrDigit)) { ... }

The Char.IsLetterOrDigit returns true for characters that are categorised in Unicode as UppercaseLetter, LowercaseLetter, TitlecaseLetter, ModifierLetter, OtherLetter, or DecimalDigitNumber.

Regex: how to find a string followed by a non alphanumeric

It sounds to me like what you're actually trying to do here is perform an exact word match. Not necessarily "a string followed by a non-alphanumeric".

You can achieve this with the \b "word boundary" regex anchor:

$search = "dog"
preg_replace("/\b".$search."\b/i", "", $str);

Find non-alphanumeric characters with alphanumeric character anywhere before

You can use

re.sub(r'(?<=[^\W_])[\W_]+(?=[^\W_])', ' ', text)

Details:

  • (?<=[^\W_]) - a letter or digit should be immediately on the left
  • [\W_]+ - one or more non-alphanumeric
  • (?=[^\W_]) - a letter or digit should be immediately on the right.

See the regex demo.

See the Python demo:

import re
texts = ['This%%is$Matrix%%$script', 'This$#is% Matrix# %!']
for text in texts:
print(re.sub(r'(?<=[^\W_])[\W_]+(?=[^\W_])', ' ', text))

Output:

This is Matrix script
This is Matrix# %!

Regex match if String has no alphanumic characters

This matches strings containing no letters or digits:

^[^a-zA-Z0-9]*$

Regular expression visualization

Debuggex Demo

And this matches strings with at least one letter or digit:

^.*[a-zA-Z0-9].*$

Regular expression visualization

Debuggex Demo

Please take a look through the Stack Overflow Regular Expressions FAQ for some more helpful information.

Test if a string contains only whitespace and non-alphanumeric characters

Both your regex and syntax is incorrect. You need not use regex at all, just need glob for this:

checkStr() {
[[ $1 != *[a-zA-Z0-9]* ]]
}

This will return false if even one alphanumeric is found in the input. Otherwise true is returned.

Test it:

$> if checkStr 'abc'; then
echo "true case"
else
echo "false case"
fi
false case

$> if checkStr '=:() '; then
echo "true case"
else
echo "false case"
fi
true case

$> if checkStr '=:(x) '; then
echo "true case"
else
echo "false case"
fi
false case

Java regex: check if word has non alphanumeric characters

Change your regex to:

.*\\W+.*


Related Topics



Leave a reply



Submit