You are on page 1of 2

Regular Expressions Methods in Python

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()

re.match(pattern, string, flags=0)

This expression is used to match a character or set of characters at


the beginning of a string. It's also important to note that this expression will
only match at the beginning of the string and not at the beginning of each line
if the given string has multiple lines.

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):

return 'Found a match!'

else:

return('Not matched!')

print(text_match("The quick brown fox jumps over the lazy dog."))

print(text_match("Python_Exercises_1"))

re

def text_match(text):

patterns = '^[a-zA-Z0-9_]*$'

if re.search(patterns, text):

return 'Found a match!'

else:

return('Not matched!')

print(text_match("The quick brown fox jumps over the lazy dog."))

print(text_match("Python_Exercises_1"))

You might also like