Enable Dark Mode!
python-regex-common-methods-in-the-python-re-module.jpg
By: Aswani VT

Python RegEx & Common Methods in the Python ‘re’ Module

Technical

Regex is the abbreviation for ‘Regular Expressions' It is a sequence of characters that forms a search pattern. Python Regex is handled by the module ‘re.’ There are various functions defined in this module to handle regular expressions. Regular expressions are specified using metacharacters. Let’s see how some of the metacharacters are interpreted by the Regex engine. 
1) Square brackets [ ]
The characters you wish to match are given within the square bracket.
[xyz]for 'x'One Match
[xyz]for 'xy'Two Match
[xyz]for 'be happy'No Match
[xyz]for 'xyz xy'Five Match
Specify the range as [x-y], which is the same as [xyz].
Specify the compliment using the caret symbol as [^xyz], which will match any character except x,y, and z.
2) Period
Single periods match single characters and so on.
--for 'p'

No Match

--for 'pq'One Match
--for 'pqr'One Match
--for 'pqrs'Two Match
3) Caret ^
Checks whether a given string starts with the given pattern.
^pfor 'p'One Match
^pfor 'pq'One Match
^pfor 'qrp'No Match
^pqfor 'pqr'One Match
^pqfor 'rqr'No Match
4) Dollar $
Checks whether the given string ends with the given pattern.
y$for 'pay'One Match
y$for 'y'One Match
y$for 'xyz'No Match
5) Star *
Matches pattern left to it if it is present zero or more times.
ab*cfor 'abc'One Match
ab*cfor 'abbc'One Match
ab*cfor 'abbbc'One Match
ab*cfor 'abxc'No Match
ab*cfor 'xabc'One Match
6) Plus +
Matches pattern left to it if it is present one or more times.
ab+cfor 'ac'No Match
ab+cfor 'abc'One Match
ab+cfor 'abbbc'One Match
ab+cfor 'abxc'No Match
ab+cfor 'xabc'One Match
7) Question mark ?
Matches pattern left to it if it is present zero or one time.
ab?cfor 'ac'One Match
ab?cfor 'abc'One Match
ab?cfor 'abbbc'No Match
ab?cfor 'abxc'No Match
ab?cfor 'xabc'One Match
8) Braces { }
Matches pattern left to it exactly specified times.
p{2}for 'p'No Match
p{2}for 'pp'One Match
9) Alternation |
Used as alternator or ‘OR’ operator.
p|qfor 'ab'No Match
p|q

for 'apb'

One Match
p|qfor 'apbq'Two Match
10) Group ()
Groups subexpressions.
(p|q)abfor 'xab'No Match
(p|q)abfor 'pab'One Match
11) Backslash \
Used to escape characters including metacharacters.
Special Sequences
\AMatches at the starting of a string
\bMatches if in the starting or ending
\BMatches if not in the starting or ending
\dSame as [0-9]
\DSame as the opposite of [0-9] or
\sMatches whitespace character
\SMatches non-whitespace character
\wMatches alphanumeric
\WMatches non-alphanumeric
\ZMatches if characters at the end
In the case of python regex, it is handled by the ‘re’ module using various functions and constants. It is imported using the following command,
import re
Some of the functions defined in the ‘re’ module are,
1) findall( )
2) search( )
3) split( )
4) sub( ) 
5) Match object
1) findall( )
All matches are returned in a list.
import re
text = "Life is good"
y = re.findall("fe", text)
print(y)
The output is [‘fe’]. If no match returns an empty list.
2) search( )
The search( ) function returns the match as an object(match object)  if a match is found for the given pattern. If no match is found, it returns none.
import re
text = "Life is good"
y = re.search("fe", text)
if y:
    print("Match found")
else :
    print("No match found")
The output is  ‘Match found’.
3) split( )
Returns the list of strings where splitting occurred. If a split pattern is not found, return the original string in the list.
import re
text = "Life is good"
y = re.split("\s", text)
print(y)
The output is ['Life', 'is', 'good']
4) sub( )
Replaces or substitutes the matched pattern with the given string.
import re
text = "The rain in Spain"
y = re.sub("i", "9", text)
print(y)
The output is ‘The ra9n 9n Spa9n’
5) Match Object
A match object is returned by a search( )  function. The match object contains the information of that particular search.
import re
text = "Life is good"
y = re.search("fe", text)
print(y)
The output is  <re.Match object; span=(2, 4), match='fe'>
Some of match object methods are  .span( ), .string,  .group( ) etc
import re
#Search for an raw string "d" character in the given words, and print its starting and ending position of first occurrence:
text = "Life is good"
y = re.search(r"d", text)
print(y.span())
The output is (11, 12).
import re
#Returns the string in which searching:
text = "Life is good"
y = re.search(r"i", text)
print(y.string)
The output is Life is good
import re
#Search for an upper case "G" character in the beginning of a word, and returns the word:
text = "Life is Good"
y = re.search(r"\bG\w+", text)
print(y.group())
The output is ‘Good’.
We have discussed above the most common regular expressions and the common methods defined in the python ‘re’ module.


If you need any assistance in odoo, we are online, please chat with us.



0
Comments



Leave a comment



whatsapp_icon
location

Calicut

Cybrosys Technologies Pvt. Ltd.
Neospace, Kinfra Techno Park
Kakkancherry, Calicut
Kerala, India - 673635

location

Kochi

Cybrosys Technologies Pvt. Ltd.
1st Floor, Thapasya Building,
Infopark, Kakkanad,
Kochi, India - 682030.

location

Bangalore

Cybrosys Techno Solutions
The Estate, 8th Floor,
Dickenson Road,
Bangalore, India - 560042

Send Us A Message