AgerNic.com
WEB DEVELOPER SITE, HTML, CSS, PHP, SQL

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

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
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:
['a lot', 'a lot']


"search" Function Returns a Match object if there is a match anywhere in the string


Example:
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:
The first white-space character is located in position: 8


split() Function

Example:
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:
['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:
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:
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
ONLINE EDITOR

news templates


COLOR PICKER

news templates
This tool makes it easy to create, adjust, and experiment with custom colors for the web.


HTML Templates
news 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
news templates
Find here examples of creative and unique website layouts.


Free CSS HTML Menu
news templates
Find here examples of creative and unique website CSS HTML menu.


0
Online Editor
ONLINE EDITOR

news templates


COLOR PICKER

news templates
This tool makes it easy to create, adjust, and experiment with custom colors for the web.


HTML Templates
news 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
news templates
Find here examples of creative and unique website layouts.


Free CSS HTML Menu
news templates
Find here examples of creative and unique website CSS HTML menu.