Table of Contents
Introduction
The task is to find the special characters in the string and if it exists reject the string otherwise accept the string.
Program
import re input_str = input("Enter teh string : ") #Passing char set as argument in compile method reg_obj = re.compile('[@_!#$%^&*()<>?/\|}{~:]') #Search reg_obj in input string if(reg_obj.search(input_str) == None): print(" '{0}' is accepted! It ahs no special character!".format(input_str)) else: print(" '{0}' is not accepted! It has special characters!".format(input_str))
Output
Explanation
In the above python code we have passed the set of special characters in compile function of regex. The search function in if condition will check whether the special character is found in the input string. If the character is found, the string is not accepted else it is accepted.
0 Comments