Python RegEx
Python Tutorial » Python RegEx
RegEx (Regular Expression) is a sequence of characters what defines a search pattern.
Pattern is: any five letter string starting with a and ending with s.
Example:
^a...s$
How we use RegEx in Python - example
Python has a module re working with RegEx.
Method used returns a match object if the search is successful.
Example:
Output:
YES! We have a match! my_string: Now with a lot of web pages I can learn PYTHON from home
import re
#Check if the string starts with "Now" and ends with "home":
my_txt = "Now with a lot of web pages I can learn PYTHON from home"
y = re.search("^Now.*home$", my_txt)
if y:
print("YES! We have a match! my_string: Now with a lot of web pages I can learn PYTHON from home")
else:
print("No match")
#Check if the string starts with "Now" and ends with "home":
my_txt = "Now with a lot of web pages I can learn PYTHON from home"
y = re.search("^Now.*home$", my_txt)
if y:
print("YES! We have a match! my_string: Now with a lot of web pages I can learn PYTHON from home")
else:
print("No match")
Output:
YES! We have a match! my_string: Now with a lot of web pages I can learn PYTHON from home
RegEx Functions
findall Returns a list containing all matches
search Returns a Match object if there is a match anywhere in the string
split Returns a list where the string has been split at each match
sub Replaces one or many matches with a string
The findall() Function - Returns a list containing all matches
Example: findall() Function
['a lot', 'a lot']
import re
#Check if a list contain "a lot":
my_txt = "Now with a lot of web pages I can learn a lot of PYTHON from home"
y = re.findall("a lot", my_txt)
if y:
print(y)
else:
print("No match")
Output:#Check if a list contain "a lot":
my_txt = "Now with a lot of web pages I can learn a lot of PYTHON from home"
y = re.findall("a lot", my_txt)
if y:
print(y)
else:
print("No match")
['a lot', 'a lot']
"search" Function Returns a Match object if there is a match anywhere in the string
Example:
The first white-space character is located in position: 8
import re
my_txt = "Now-with a lot of web pages I can learn a lot of PYTHON from home"
y = re.search("\s", my_txt)
print("The first white-space character is located in position:", y.start())
Output:my_txt = "Now-with a lot of web pages I can learn a lot of PYTHON from home"
y = re.search("\s", my_txt)
print("The first white-space character is located in position:", y.start())
The first white-space character is located in position: 8
split() Function
Example:
['Now-with', 'a', 'lot', 'of', 'web', 'pages', 'I', 'can', 'learn', 'a', 'lot', 'of', 'PYTHON', 'from', 'home']
import re
#Split the string at every white-space character:
my_txt = "Now-with a lot of web pages I can learn a lot of PYTHON from home"
y = re.split("\s", my_txt)
print(y)
Output:#Split the string at every white-space character:
my_txt = "Now-with a lot of web pages I can learn a lot of PYTHON from home"
y = re.split("\s", my_txt)
print(y)
['Now-with', 'a', 'lot', 'of', 'web', 'pages', 'I', 'can', 'learn', 'a', 'lot', 'of', 'PYTHON', 'from', 'home']
sub() Function
Example: Create an alias for my_file called filexx:
Now-with--a--lot--of--web--pages--I--can--learn--a--lot--of--PYTHON--from--home
import re
#Replace all white-space characters with the digit "--":
my_txt = "Now-with a lot of web pages I can learn a lot of PYTHON from home"
y = re.sub("\s", "--", my_txt)
print(y)
Output:#Replace all white-space characters with the digit "--":
my_txt = "Now-with a lot of web pages I can learn a lot of PYTHON from home"
y = re.sub("\s", "--", my_txt)
print(y)
Now-with--a--lot--of--web--pages--I--can--learn--a--lot--of--PYTHON--from--home
python regex match, online, replace, search, group, split, findall, test, flags, re.natch python, regex online, re.sub, match string with regex, find in string, regex all matches, extract from string
Python RegEx - python
Online Editor
This tool makes it easy to create, adjust, and experiment with custom colors for the web.
HTML Templates

Magnews2 is a modern and creative free magazine and news website template that will help you kick off your online project in style.
CSS HTML Layout

Find here examples of creative and unique website layouts.
Free CSS HTML Menu

Find here examples of creative and unique website CSS HTML menu.