Functions in ‘re’ module(Part II)-findall(), split(), sub()
Earlier post explains :
1. Match()
2. search()
This post will explain about:
3. findall()
4. split()
5.sub()
findall()
It finds all sub-strings where the pattern matches and returns them in a list.
Syntax
re.findall(pattern,string,flags)
Where,
pattern – It is the regular expression to be found
String – It is the data string , which is checked to search the pattern anywhere in the string.
Flags – This parameter is optional.It is used to determine or control behavior of patterns.At a time we can specify multiple flags by using bitwise OR (|).
Example :
""" Author : ITVoyagers (https://itvoyagers.in/) Date :30th January 2019 Description : Program to show use of findall() """ import re var="The bat and a fat rat sat on a cat" res=re.findall("[bfrsa]at",var) print(res)
OUTPUT
['bat', 'fat', 'rat', 'sat']
Note : We cannot use groups() as list is returned as output and it does not support groups()
split()
It can be used to split a string into a list of sub-strings.
Syntax
re.split(sep,max_slpit)
Where,
sep – (optional parameter) it represents separator. If nothing is mentioned then string will be separated into sub-strings by using whitespaces as delimiters
max_split – (optional parameter) it determines the number of splits to be made.
Example :
""" Author : ITVoyagers (https://itvoyagers.in/) Date :30th January 2019 Description : Program to show use of split() """ import re value="hey visitors, this is a program for split function" #split() without parameters res=value.split() print ( res) #split() with sep parameter res=value.split(',') print ( res) #split() with sep parameter res=value.split(' ',1) #space given as separator & 1 indicates maximum 1 split will be made print ( res)
OUTPUT
['hey', 'visitors,', 'this', 'is', 'a', 'program', 'for', 'split', 'function'] ['hey visitors', ' this is a program for split function'] ['hey', 'visitors, this is a program for split function']
Note : We cannot use res.groups() as list is returned as output and it does not support groups()
sub()
It can be used to match regular expression and replace it with desired string.
Syntax
re.sub(pattern,replacement,string,count)
Where,
pattern – it is used to match the regex
replacement – this is replacement string to be used
string – string in which pattern will be match and replacement string will be replaced
count – maximum count of matches to be replaced. By default its 0 means replaces all occurances.
Example :
""" Author : ITVoyagers (https://itvoyagers.in/) Date :30th January 2019 Description : Program to show use of sub() """ import re date="2019-01-30" pat=re.sub('-',' ',date) #it will replace - with space print(pat) op=re.sub('-',' ',date,1) #it will replace - with space but only for 1 occurance print(op) non_dig=re.sub('-',': ',date) print(non_dig)
OUTPUT
2019 01 30 2019 01-30 2019: 01: 30
You can also check following other posts on regular expression using python here