Program to check if a string contains any special character
Given a string, the task is to check if that string contains any special character (defined special character set). If any special character found, don’t accept that string.
Examples :
Input : Geeks$For$Geeks Output : String is not accepted. Input : Geeks For Geeks Output : String is accepted
Approach : Make a regular expression(regex) object of all the special characters that we don’t want, then pass a string in search method. If any one character of string is matching with regex object then search method returns a match object otherwise return None.
Below is the implementation :
Python3
# Python program to check if a string # contains any special character # import required package import re # Function checks if the string # contains any special character def run(string): # Make own character set and pass # this as argument in compile method regex = re.compile('[@_!#$%^&*()<>?/\|}{~:]') # Pass the string in search # method of regex object. if(regex.search(string) == None): print("String is accepted") else: print("String is not accepted.") # Driver Code if __name__ == '__main__' : # Enter the string string = "Geeks$For$Geeks" # calling run function run(string) |
PHP
<?Php // PHP program to check if a string // contains any special character // Function checks if the string // contains any special character function run($string) { $regex = preg_match('[@_!#$%^&*()<>?/\|}{~:]', $string); if($regex) print("String is accepted"); else print("String is not accepted."); } // Driver Code // Enter the string $string = 'Geeks$For$Geeks'; // calling run function run($string); // This code is contributed by Aman ojha ?> |
Output :
String is not accepted.
Recommended Posts:
- Python | Check order of character in string using OrderedDict( )
- Python program for removing i-th character from a string
- Python program to find occurrence to each character in given string
- Python Regex | Program to accept string ending with alphanumeric character
- Python program to check if a given string is Keyword or not
- Python program to check if given string is pangram
- Python program to check if a string is palindrome or not
- Python program to check if string is empty or not
- Python program to check if a string contains all unique characters
- Python program to check if given string is vowel Palindrome
- Python | Lowercase first character of String
- Python | Frequency of each character in String
- Count occurrences of a sub-string with one variable character
- Python | Count occurrences of a character in string
- Python | Split string on Kth Occurrence of Character
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.
Improved By : Aman ojha



