#!/usr/bin/env python # coding: utf-8 # # RE module # In[1]: import re import numpy as np # ### 1. To extract a pattern in a given string - Use `re.findall(pattern, text)` # # Returns a list containing all matches. # # In the example below we try to extract string of three consecutive numbers. # In[2]: text = 'Jeswin 619 George 999 98Y Y27J0 JK871' re.findall(r'\d{3}', text) # Now to get the first or last patterns use indexing. # In[3]: re.findall(r'\d{3}', text)[0] # In[4]: re.findall(r'\d{3}', text)[-1] # ### 2. To substitute a pattern with a string of your choice - `re.sub(pattern, desired_string, text)` # # replaces the matches with the text of your choice. # In[5]: text = 'Jeswin 619 George 999 98Y Y27J0 JK871' # To replace __Y__ with string __B__. # In[6]: re.sub('Y', "B", text) # In the given text, ensure that the alpha-numeric words doesnt have alphabets and resulting numeric word must have only numerics and must have length of 3 (You can use any numeric of your choice). # In[7]: output = [] for word in text.split(): if re.findall(r'\d', word): if re.findall(r'[A-Z]',word): word = re.sub(r'[A-Z]', '5', word) if len(word)==3: output.append(word) elif len(word)<3: while (3-len(word))<=0: word += str(np.random.randomint(9)) output.append(word) elif len(word)>3: word = word[:3-len(word)] output.append(word) else: output.append(word) else: output.append(word) # In[8]: output # Combining the list of words to a single sentence using [string join()](https://www.geeksforgeeks.org/python-string-join-method/). # In[9]: ' '.join(output) # In[10]: text # # Reference # # ![](images/1.PNG) # # ![](images/2.PNG) # # ![](images/3.PNG)