Python Regular expression Part 1

Python Regular expression
A regular expression is a sequence of characters having a special meaning and can be used to find or match a pattern in string values. 

The module re provides support for Regular Expressions in Python. 

The below program accepts a string as an input value and prints only the digits in it. 


import re 

inputvalue = input() 
results = re.findall("[\d]", inputvalue) 
for r in results: 
print (r,end="") 

- If the input is how43kij98aa67 the output is 439867 
- If the input is 2328732873mkxcjxc20392039 the output is 232873287320392039

Regular Expressions - ^ 
The ^ in the pattern acts as a negation. 
The below program prints all characters in the input string except the digits from 0 to 9. 

import re 
pattern = "[^\d]" 
inputvalue = input()
results = re.findall(pattern, inputvalue) 
for r in results:
    print (r,end="") 

- If the input is how43k_*!ij98a(a67cc)kslk123 the output is howk_*!ija(acc)kslk

Regular Expressions r"RE" 

To denote more sophisticated patterns, we will use r"RE" as the pattern henceforth.
As an example the below program accepts an input value and prints only the digits in it. 


import re 
inputvalue = input() 
results = re.findall(r"\d", inputvalue) 
for r in results: 
print (r,end="") 

- If the input value is getupat5:30pm then the output is 530

Regular Expressions + 

The + in a RE pattern mandates that the preceding RE must at least occur once (that is one or more times). 

Let us say, we wish to accept a string value and print all the occurrences where a is followed by one or more b's. The program below uses + to achieve the same. 

import re 
inputvalue = input() 
results = re.findall(r"ab+", inputvalue) 
for r in results: 
print (r,end="") 

- If the input value is abcdabefbaxy then the output is abab 
- If the input value is abcdabbbefbaxy then the output is ababbb 
- If the input value is babbcdbaghabbbxyababzz then the output is abbabbbabab

Regular Expressions {m} 

The {m} in a RE pattern mandates that the preceding RE must at least occur m times. 

Let us say, we wish to accept a string value and print all the occurrences of 555. This is nothing but 5 repeated exactly 3 times. The program below uses {3} to achieve the same. 

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

- If the input value is wegot555pencilstobedistributedto55studentsandthetotalcostwasrs5555 then the output is 555555 Please note that in the 5555 towards the end only 555 is printed.

Regular Expressions | 

The | is used to indicate OR condition. 

Let us say, we wish to accept a string value and print all the occurrences of aa or bb or cc in it. The program below uses a{2}|b{2}|c{2} to achieve the same. 

import re 
inputvalue = input() 
results = re.findall(r"a{2}|b{2}|c{2}", inputvalue) 
for r in results: 
print (r,end="") 

- If the input value is haajjbbmmcckoabcabcpoaabbbbcc then the output is aabbccaabbbbcc


Regular Expressions {m,n} 

The {m,n} is used to match the occurrence of the previous RE m to n times. It is a greedy matching (That is a{3,5} will match aaaaaa as aaaaa and NOT as aaa aaa) 

Let us say, we wish to accept a string value and print all the occurrences of bb or bbb or bbbb in it. The program below uses b{2,4} to achieve the same.

import re 
inputvalue = input() 
results = re.findall(r"b{2,4}", inputvalue) 
for r in results: 
print (r,end=" ") 

- If the input value is abbcdbbbghbbbbbbkiol then the output is bb bbb bbbb bb


                        Next Page

Comments