17_Regular Expression
17_Regular Expression
Learning objective
• What is a Regular Expression?
• Metacharacters
• match() function
• search() function
• re.match() vs re.search()
• findall() function
• split() function
• sub() function
What is a Regular Expression?
• A regular expression RegEx is a special sequence of characters that
helps you match or find other strings or sets of strings, using a
specialized syntax held in a pattern.
• This can make cleaning and working with text-based data sets
much easier, saving you the trouble of having to search through
mountains of text by hand.
Metacharacters
• To understand the RE analogy, Metacharacters are useful,
important and will be used in functions of module re.
• There are many metacharacters available in re module.
#Example:
import re
txt = "The rain in Spain"
x = re.search("Portugal", txt)
print(x)
re.match() vs re.search()
• There is a difference between the use of both functions.
print(f"Replaced 'than' with 'then' in the text. Updated file saved to {output_file_path}.")
You must have learnt:
• What is a Regular Expression?
• Metacharacters
• match() function
• search() function
• re.match() vs re.search()
• findall() function
• split() function
• sub() function