top of page
learn_data_science.jpg

Data Scientist Program

 

Free Online Data Science Training for Complete Beginners.
 


No prior coding knowledge required!

Python Regular Expressions

Writer's picture: Arpan SapkotaArpan Sapkota

The regular expressions can be defined as the sequence of characters which are used to search for a pattern in a string. The module re provides the support to use regex in the python program. The re module throws an exception if there is some error while using the regular expression.


The re module must be imported to use the regex functionalities in python.

import re

Regex Functions

The following regex functions are used in the python.

1 match - This method matches the regex pattern in the string with the optional flag. It returns true if a match is found in the string otherwise it returns false.

2 search - This method returns the match object if there is a match found in the string.

3 findall - It returns a list that contains all the matches of a pattern in the string.

4 split - Returns a list in which the string has been split in each match.

5 sub - Replace one or many matches in the string.

Forming a regular expression

A regular expression can be formed by using the mix of meta-characters, special sequences, and sets.


The findall() function

This method returns a list containing a list of all matches of a pattern within the string. It returns the patterns in the order they are found. If there are no matches, then an empty list is returned.

Consider the following example.


Example

import re  
str = "Who are you. Who is someone"  
matches = re.findall("Who", str)  
print(matches)   

The match object

The match object contains the information about the search and the output. If there is no match found, the None object is returned.


Example

import re  
str = "Who are you. Who is someone"  
matches = re.search("Who", str)  
print(type(matches))  
print(matches) #matches is the search object  

The Match object methods

There are the following methods associated with the Match object.

span(): It returns the tuple containing the starting and end position of the match.

string(): It returns a string passed into the function.

group(): The part of the string is returned where the match is found.


Example

import re  
matches = re.search("Who", str)  
print(matches.span())  
print(matches.group())  
print(matches.string)  

0 comments

Recent Posts

See All

Commentaires


COURSES, PROGRAMS & CERTIFICATIONS

 

Advanced Business Analytics Specialization

Applied Data Science with Python (University of Michigan)

Data Analyst Professional Certificate (IBM)

Data Science Professional Certificate (IBM)

Data Science Specialization (John Hopkins University)

Data Science with Python Certification Training 

Data Scientist Career Path

Data Scientist Nano Degree Program

Data Scientist Program

Deep Learning Specialization

Machine Learning Course (Andrew Ng @ Stanford)

Machine Learning, Data Science and Deep Learning

Machine Learning Specialization (University of Washington)

Master Python for Data Science

Mathematics for Machine Learning (Imperial College London)

Programming with Python

Python for Everybody Specialization (University of Michigan)

Python Machine Learning Certification Training

Reinforcement Learning Specialization (University of Alberta)

Join our mailing list

Data Insight participates in affiliate programs and may sometimes get a commission through purchases made through our links without any additional cost to our visitors.

bottom of page