How to check if a string contains a special character (!@#$%^&*()_+)
Match it against a glob. You just have to escape the characters that the shell otherwise considers special:
#!/bin/bash
str='some text with @ in it'
if [[ $str == *['!'@#\$%^\&*()_+]* ]]
then
echo "It contains one of those"
fi
Check is string contains special characters and at least 2 characters amongs digit and letter
You can assert the 2 occurrences of a character or digit in the same lookahead, and then match at least a single "special" character.
Using a case insensitive pattern:
^(?=(?:[^a-z\d\n]*[a-z\d]){2})[a-z\d]*[~!@#$%^&*()_+<>•`{}\\][~!@#$%^&*()_+<>•`{}\\a-z\d]*$
The pattern matches:
^
Start of string(?:[^a-z\d\n]*[a-z\d]){2}
Assert 2 occurrences of either a char a-z or a digit. The[^a-z\d\n]*
part negates the character class using[^
to prevent unnecessary backtracking[a-z\d]*
Match optional chars a-z or a digit[~!@#$%^&*()_+<>•`{}\]
Match a special character[~!@#$%^&()_+<>•`{}\a-z\d]
Match optional allowed chars$
End of string
Regex demo
How to find whether a string contains any of the special characters?
Regex RgxUrl = new Regex("[^a-z0-9]");
blnContainsSpecialCharacters = RgxUrl.IsMatch(stringToCheck);
Check for special characters (/*-+_@&$#%) in a string?
The easiest way it to use a regular expression:
Regular Expression for alphanumeric and underscores
Using regular expressions in .net:
http://www.regular-expressions.info/dotnet.html
MSDN Regular Expression
Regex.IsMatch
var regexItem = new Regex("^[a-zA-Z0-9 ]*$");
if(regexItem.IsMatch(YOUR_STRING)){..}
Check for special characters in string
I suggest using RegExp .test()
function to check for a pattern match, and the only thing you need to change is remove the start/end of line anchors (and the *
quantifier is also redundant) in the regex:
var format = /[ `!@#$%^&*()_+\-=\[\]{};':"\\|,.<>\/?~]/;// ^ ^ document.write(format.test("My@string-with(some%text)") + "<br/>");document.write(format.test("My string with spaces") + "<br/>");document.write(format.test("MyStringContainingNoSpecialChars"));
Related Topics
Git Post-Receive Checkout to Remote Machine
Restart Process on File Change in Linux
Cross-Compilation to X86_64-Unknown-Linux-Gnu Fails on MAC Osx
Swift on Linux: Make Very First Step Work
Why Glibc Binary Is Called Libc.So.6 Not a Libc.So.1 or Libc.So.4
What Is The Current State of Tail-Call-Optimization for F# on Mono (2.11)
Best Approach of Image Versioning in Yocto
How to Set Pthread CPU Affinity in Os X
Use Grep to Remove Words from Dictionary Whose Roots Are Already Present
Unix Cut Except Last Two Tokens
Get .Net Core Dll Version on Linux
Is Anyone Using Netlink for Ipc
How to Create a File Listener in Linux
Linux Bash Script Get User Input and Store in a Array