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

xralf

I tried the following solution.

if !(($str =~ ^.*[a-zA-Z0-9].*$)); then
  continue;
fi

It seems there's something wrong with the syntax.

anubhava

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

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Check if string contains only non-alphanumeric characters

Test if string has non whitespace characters in Bash

Test if string contains only characters

better way to ensure that a string contains only alphanumeric characters?

How can I check to see if a string contains characters of whitespace/alphanumeric/etc?

Test UITextField text string to only contain alphanumeric characters

Test if all characters in string are not alphanumeric

Count only alphanumeric characters in a string

Check if NSString contains alphanumeric + underscore characters only

Check if string contains only whitespace

Replace all non-alphanumeric characters in a string

Replace all non-alphanumeric characters in a string

Removing non alphanumeric characters from a string in C

Regex test in JavaScript if a string contains only unique characters

how to test a string that contains only letter characters and dash(-) [javascript]

How can I check if string contains characters & whitespace, not just whitespace?

Swift - Getting only AlphaNumeric Characters from String

Check if a string has only whitespace characters in C

Check if a string is empty or only whitespace characters in racket

Check if String only contains these characters {([])}

Check if a string only contains the characters > or < or -

A string contains certain characters only

Most efficient regex for checking if a string contains at least 3 alphanumeric characters

How to drop and keep only certain non alphanumeric characters?

Only sub out non-alphanumeric characters in regex

sed: remove all non-alphanumeric characters inside quotations only

Validate an alphanumeric string that contains a fixed amount of non-consecutive periods

Check if String contains alphanumeric

How to check if a string contains any kid of whitespace between characters JavaScript?