Regular expressions (REs, or regexes, or regex patterns) are used to identify whether a pattern exists in a given sequence of strings or not. For example validation of Email, phone number, and passwords, etc. Python Regex is basic a tiny, highly specialized programming language embedded inside Python, which is available through the
The regular expressions are extremely powerful and useful, you will learn how to use them in Python in this tutorial.
Basically used at the server side to validate the format of email addresses or phone numbers or passwords during registration. Another use of the Python re module is parsing text data files to find, replace or delete certain strings, etc.
Python Regex Syntax
Simple String matching Regular Expression Example :
Here is importing a python re module and matching the strings.
import re pattern = r"eyehunt" sequence = "eyehunt" if re.match(pattern, sequence): print("Matched!") else: print("Not a matched!")
Python regular expression methods
In python “re” module provides a several inbuilt functions to perform an option.
- re.match() – Determine if the RE matches at the beginning of the string.
- re.search() – Scan through a string, looking for any location where this RE matches.
- re.findall() – Find all substrings where the RE matches, and return them as a list.
Python re.match() Function Example :
Matching the word in a string.
import re msg = "split the string in python" print(re.match("split",msg))
Output : <re.Match object; span=(0, 5), match=’split‘>
Python re.search() Function Example :
Search normal word in a string.
import re msg = "search the string in python" print(re.search("in", msg))
Output : <re.Match object; span=(14, 16), match=’in‘>
Python re.findall() Function Example :
Email example – Using regex to find the email pattern in a string.
import re msg = "search the string in python [email protected]" print(re.findall('[\w\.-][email protected][\w\.-]+', msg))
Output : [‘[email protected]’]
\dMatches any decimal digit; this is equivalent to the class
\DMatches any non-digit character; this is equivalent to the class
\sMatches any whitespace character; this is equivalent to the class
\SMatches any non-whitespace character; this is equivalent to the class
\wMatches any alphanumeric character; this is equivalent to the class
\WMatches any non-alphanumeric character; this is equivalent to the class
Modifying Strings Functions in Python re
Regular expressions are also commonly used to modify strings in various ways, using the following pattern functions:
- split() – Split the string into a list, splitting it wherever the RE matches
- sub() – Find all substrings where the RE matches, and replace them with a different string
- subn() – Does the same thing as
sub(), but returns the new string and the number of replacements
Python re – re.split function
\s – Matches whitespace. Equivalent to [\t\n\r\f].
import re msg = "split the string in python" print(re.split("\s", msg))
Output : [‘split’, ‘the’, ‘string’, ‘in’, ‘python’]
Python Search and Replace –
sub() the method takes a replacement value, which can be either a string or a function, and the string to be processed.
import re msg = "search the string in python" print(re.sub('string', 'number', msg))
Output: search the number in python
subn() the method does the same work, but returns a 2-tuple containing the new string value and the number of replacements that were performed:
import re msg = "search the string in python" print(re.subn('string', 'number', msg))
Output : (‘search the number in python’, 1)
Reference website: https://docs.python.org/3/howto/regex.html
Do comment if you have nay doubts and suggestion on this tutorial.
Note: This example (Project) is developed in PyCharm 2018.2 (Community Edition)
JVM: OpenJDK 64-Bit Server VM by JetBrains s.r.o
All Python Regex Examples are in Python 3, so it may change its different from python 2 or upgraded versions.
Degree in Computer Science and Engineer: App Developer and has multiple Programming languages experience. Enthusiasm for technology & like learning technical.