In this Python article we are going to learn about Find Special Characters in Python with re (Regular
Expression ). for this we are using Python re, so regular expression (or RE) specifies a set of strings
that matches it, the functions in this module let you check if a particular string matches a given
regular expression (or if a given regular expression matches a particular string, which comes down
to the same thing). for more information you can check Regular Expression documentation. also
a special character is a character that is not an alphabetic or numeric character. Punctuation
marks and other symbols are examples of special characters like @, #, $, %, &.
Also you can read more Python GUI articles in the below links.
1: Kivy GUI Development Tutorials
2: Python TKinter GUI Development
5: PyQt5 GUI Development Course
So now this is the code for Find Special Characters in Python with re.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
import re #our function def find_charecter(text): #regular expression with compiled method regex = re.compile('[@_!#$%^&*()<>?/\|}{~:]') #check for the charecter if(regex.search(text) == None): print("There is no special charecter in : ", text) else: print("There is special charecter in : ", text) text = "#codeloop@@@" find_charecter(text) |
At the top first of all we have imported our re class from python, and after that we have
created a method that takes a parameter.
After that we have used re.compile(), it is a regular expression pattern into a regular expression object, which can be used for matching using its match()
, search()
and other methods.
1 |
regex = re.compile('[@_!#$%^&*()<>?/\|}{~:]') |
This is used for checking the characters.
1 2 3 4 5 |
if(regex.search(text) == None): print("There is no special charecter in : ", text) else: print("There is special charecter in : ", text) |
So now if you run the code this will be the result.
1 2 3 |
There is special charecter in : #codeloop@@@ Process finished with exit code 0 |
And If you remove the characters, this will be the result.
1 2 3 |
There is no special charecter in : codeloop Process finished with exit code 0 |
Subscribe and Get Free Video Courses & Articles in your Email