Jun 24, 2018

Python regular expressions

import re

print '-----------------regex search/match/compile---------------------'
match = re.search('hello', 'hi hello world')
if match:
    print match.group()
else:
    print 're.search not found'

match = re.match('hello', 'hi hello world')
if match:
    print match.group()
else:
    print 're.match not found'

match = re.compile('hello').match('hello world')
if match:
    print match.group()  #hello
else:
    print 're.compile not found'

print '-----------------regex search---------------------'
#Case Insensitive Search
str = "Mother Teresa"
print("Input sting : " +  str);
match = re.search(r"mother", str, re.IGNORECASE)  #Case Insensitive Search
if (match):
    print("Matched string : " +  match.group())   
#Matched string : Mother
else:
    print("NOT Matched");   


print '-----------------regex findall in a string---------------------'
str1 = 'Send upport related queries to support@organization.com, send admin related queries to admin@organization.com'
emails = re.findall(r'[\w\.-]+@[\w\.-]+', str1)
for email in emails:
    print email

Output:
support@organization.com
admin@organization.com   

print '-----------------regex findall in file---------------------'
f = open('regex.txt', 'r')
strings = re.findall(r'Python', f.read())
for string in strings:
    print('Each String : ', string)

Output:
('Each String : ', 'Python')
('Each String : ', 'Python')
('Each String : ', 'Python')
('Each String : ', 'Python')

print '-----------------regex groups---------------------'
str = "alice@gmail.com"
match = re.match(r'([\w.-]+)@([\w.-]+)', str)
if match:
    print('Match found: ', match.group())
    print('Match found: ', match.group(1))
    print('Match found: ', match.group(2))
else:
    print('No match')

Output:

('Match found: ', 'alice@gmail.com')
('Match found: ', 'alice')
('Match found: ', 'gmail.com')

print '-----------------regex compile---------------------'
# Case Insensitive Search using compile
# Compile is more useful in loops & iterations, this will avoid re-building regex everytime
str = "Mother Teresa"
print("\n Input Str : ", str)
match = re.compile(r"mother ", re.IGNORECASE)  #Case Insensitive Search
if (match.search(str)):
    print("Matched Str")
else:
    print("NOT Matched Str")



Output:

-----------------regex search/match/compile---------------------
hello
re.match not found
hello
-----------------regex search---------------------
Input sting : Mother Teresa
Matched string : Mother
-----------------regex findall in a string---------------------
support@organization.com
admin@organization.com
-----------------regex findall in file---------------------
('Each String : ', 'Python')
('Each String : ', 'Python')
('Each String : ', 'Python')
('Each String : ', 'Python')
-----------------regex groups---------------------
('Match found: ', 'alice@gmail.com')
('Match found: ', 'alice')
('Match found: ', 'gmail.com')
-----------------regex compile---------------------
('\n Input Str : ', 'Mother Teresa')
Matched Str

No comments:

Post a Comment