Showing posts with label python_re_match. Show all posts
Showing posts with label python_re_match. Show all posts

Sep 18, 2019

python timeit re vs compiled re

import re
import timeit

s = 'strings are strings'
compiled_regex = re.compile(r'(str)in(gs)')

def not_compiled_func():
   r = re.match(r'(str)in(gs)', s) 
   #print(r.group())

def compiled_func():
  r = compiled_regex.match(s)
  #print(r.group()) 

t1 = timeit.timeit(stmt=not_compiled_func, number=1000000)
print('%0.2f' % t1)  #5.65 secs
t2 = timeit.timeit(stmt=compiled_func, number=1000000)
print('%0.2f' % t2)  #1.73 secs



Jan 25, 2019

Python re subn

import re

text = 'python is good, python is better, python is best';

#subn - returns a tuple with no of substitutions made

print re.subn('python', 'PYTHON', text, count=0, flags=0) #replaces all
#('PYTHON is good, PYTHON is better, PYTHON is best', 3)

print re.subn('python', 'PYTHON', text, count=1, flags=0) #replaces first
#('PYTHON is good, python is better, python is best', 1)

print re.subn('python', 'PYTHON', text, count=2, flags=0) #replaces first & second
#('PYTHON is good, PYTHON is better, python is best', 2)

print re.subn('python', 'PYTHON', text, count=3, flags=0) #replaces first, second, third
#('PYTHON is good, PYTHON is better, PYTHON is best', 3)

Python substitute nth occurance

import re

##Substitue 3rd occurrence of 'python' with 'PYTHON'

nth_occurance = 3
text = 'python is good, python is better, python is best';

count = text.count('python')
if count <= 1:
print re.sub('python', r'PYTHON', text)
else:
print re.sub('^((.*?python.*?){' + str(nth_occurance-1) + '})python', r'\1PYTHON', text)


Output:
python is good, python is better, PYTHON is best

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

Mar 17, 2013

Python Regular Expressions (re.compile Vs re.match)

Today we discuss about the following topics :
1) Pre-compiled Regular Expressions - Uses & Advantages (General Concept)
2) Explain the difference between Python re.compile Vs re.match with an example.

Let's dive into the topic & continue the fun :

1) Pre-compiled Regular Expressions - Uses & Advantages (General Concept)
This is a general topic irrespective of any language (Perl or Python or any)
The extra mile of using a pre-compiled regular expression is :

     a) When you have to execute the same regular expression pattern over millions of lines (say reading lines a file), then pre-compiled regular expressions are very handy by drastically reducing the time of execution.

     b) Since the regex pattern is pre-compiled in advance, no need to process the regex pattern each and every time while reading the lines from the file (suppose millions of lines in the file)

    c) Here, we assume, the regex pattern which you have to use, should be constant, if has to vary every time then pre-compiled regex will not be that useful.

2) Explain the difference between Python re.compile Vs re.match with an example.
Let us explain the explain with a lucid example.
Here we are trying to run a regex pattern in loop of 100000 times with both Python re.compile(pre-complied) & re.match
Please check the time difference between in the output file.

E.g., regex_compile_vs_match.py

import re
import time

input_str       = "Mother Teresa"
count_compile   = 0
count_match     = 0

time_val_1 = time.time()

compiled_regex = re.compile('(\w+)\s+(\w+)')

for i in range(1000000):
    if compiled_regex.match(input_str):
        count_compile += 1

print ("Count Compile Value : ", count_compile)        
print("Time taken by Using Compile : ",  time.time() - time_val_1)
time_val_2 = time.time()

for i in range(1000000):
    if re.match('(\w+)\s+(\w+)', input_str):
        count_match += 1

print ("Count Match Value : ", count_compile)          
print("Time taken by Using Match   : ",  time.time() - time_val_2)  


Output:

Count Compile Value : 1000000
Time taken by Using Compile :  1.340999994277954
Count Math Value : 1000000
Time taken by Using Match   :  2.994999885559082  


You might wish to read Regular Expressions Concepts :
Brief on Regular Expressions
Greedy Operators in Regular Expressions in Perl
Modifiers in Regular Expressions in Perl
Capturing concept in Regular Expressions in Perl
Capture Pre Match ,Post Match, Exact match in Regular Expressions in Perl
Non Capturing Paranthesis in Regular Expressions in Perl
Substitute nth occurance in Regular Expressions in Perl
All Topics in Regular Expressions in Perl

You might also wish to read other topics like :
Python Class and Object Example
Inheritance in Python
Packages in Python
Exceptions in Python
How to remove duplicate lines from a file in Perl
How to remove duplicate lines from a file in Pyhton