Python Regular expression Part 2

Regular Expressions {m,n}? 

The {m,n}? is used to match the occurrence of the previous RE m to n times as a NON-greedy matching (That is a{3,5}? will match aaaaaa as aaa aaa and NOT as a single aaaaa containing 5 a's). That is it will match as minimum as possible. 

Let us say, we wish to accept a string value and print all the occurrences of ff in it. The program below uses f{2,3}? to achieve the same. 

import re 

inputvalue = input() 
results = re.findall(r"f{2,3}?", inputvalue) 
for r in results: 
print (r,end=" ") 

- If the input value is abcdfffghffkoolfffff then the output is ff ff ff ff

RegEx - Positive Look Behind Assertion 

The (?<=...) is used to match if the current position is preceded by ... (... refers to characters that must be preceded) 

As an example, if we wish to find the number of occurrences of abc in the input string value, we use the RE (?<=ab)c 

This will match whereever c is preceded by ab. 

Consider the program given below. 

import re 

inputvalue = input() 
count=0 
results = re.findall(r"(?<=ab)c", inputvalue) 
for r in results: 
count+=1 
print (count) 

- If the input value is sdabckijbackoolabcabc then the output is 3

RegEx - Negative Look Behind Assertion 

The (?<!...) is used to match if the current position is NOT preceded by ... (... refers to characters that must NOT be preceded) 

As an example, if we wish to find the number of occurrences of bc in the input string value which is NOT preceded by a, we use the RE (?<!a)bc 
This will match whereever bc is DOES NOT have a before it. 

Consider the program given below. 

import re 

inputvalue = input() 
count=0 
results = re.findall(r"(?<!a)bc", inputvalue) 
for r in results: 
count+=1 
print (count) 

- If the input value is abckingbccbahbc then the output is 2


RegEx - Positive Look Ahead Assertion 

The positive look ahead assertion (?=...) is used to match if the current position is succeeded by ... (... refers to characters that must be succeeded). 

As an example, if we wish to find the number of occurrences of abcd in the input string value, we use the RE ab(?=cd) This will match whereever ab is succeeded by cd. 

Consider the program given below. 

import re 

inputvalue = input() 
results = re.findall(r"ab(?=cd)", inputvalue) 
count=0 for r in results: 
count+=1 
print (count) 

- If the input value is abcdcoolercdabonemoreabcdabcdjjjabcdssabcd, the output is 5


RegEx - Negative Look Ahead Assertion 

The negative look ahead assertion (?!...) is used to match if the current position is NOT succeeded by ... (... refers to characters that must NOT be succeeded). 

As an example, if we wish to find the number of occurrences where ab is NOT succeeded by cd in the input string value, we use the RE ab(?!cd) This will match whereever ab is NOT succeeded by cd.

Consider the program given below. 

import re 

inputvalue = input() 
results = re.findall(r"ab(?!cd)", inputvalue) 
count=0 for r in results: 
count+=1 
print (count) 

- If the input value is abcdkijabcloopabdjhabuyabdsabcd, the output is 4



If you have any doubts, comment below 👇👇👇👇

Comments