Professional Documents
Culture Documents
There are several methods available to use regular expressions. Here we are
going to discuss some of the most commonly used methods and also give a
few examples of how they are used. These methods include:
1. re.match()
2. re.search()
3. re.findall()
4. re.split()
5. re.sub()
6. re.compile()
The expression below will return None because Python does not appear at the
beginning of the string.
# match.py
import re
result = re.match(r'Python', 'It\'s easy to learn Python. Python also
has elegant syntax')
Write a Python program to match a string that contains only upper and lowercase
letters, numbers, and underscores.
import import re
def text_match(text):
patterns = '^[a-zA-Z0-9_]*$'
if re.search(patterns, text):
else:
return('Not matched!')
print(text_match("Python_Exercises_1"))
re
def text_match(text):
patterns = '^[a-zA-Z0-9_]*$'
if re.search(patterns, text):
else:
return('Not matched!')
print(text_match("Python_Exercises_1"))