How to search for specific pattern in a text using Python | applied electronics engineering

Latest

How to search for specific pattern in a text using Python

By Applied Electronics - Monday, April 10, 2017 No Comments
In this tutorial you will learn how to search for specific pattern in a text using Python Programming language. Suppose that you have a text file and you want to find some specific text in it. There are two ways to write program to do this. One is using the decision constructs like if statement and the other is using regular expression. Using regular expression it is much faster method to do this. We show a simple python program to find a phone number using regular expression using python programming language.

Suppose you have some text in some text file called mymsg.txt. This file contains the following text as follows.

This is an example where we want to search for phone number 111-999-434

The text has a phone number 111-999-434. Suppose you want to check whether such phone number pattern exist in the text using regular expression. Then the process of finding such pattern using python is as follows.

1. Import re module
2. Import the text to be searched
3. Create a regex object
4. Use search method to search for the pattern
5. Do something with it like print

The step 5 depends on what you want to do once it has been found(or not found). Here we simply print it out.

The following is Python program that does this.

import re

f = open('mymsg.txt')
msg = f.read()
regexobj = re.compile(r'\d\d\d-\d\d\d-\d\d\d')
res = regexobj.search(msg)

print('Match found: '+ res.group())

The first line imports the regular expression re module. This is mandatory. Much of the in built functions for regular expression are stored in it.

Then you import the file mymsg.txt using the open method and create a file object called f. Then using the file object method read() you read in the content of the mymsg.txt file. The text is stored in msg variable. See 3 steps to reading text files in python and how to open and write a file in python if you don't know how to read files in python.

Next you would create a regex object that contains the text pattern to be processed(in this case to search). This is done using the line re.compile(). Inside the compile(), the argument is the pattern to be processed by the compile function. In this case it is \d\d\d-\d\d\d-\d\d\d where \d means digit character. The r, which means raw, is used to ignore escape character that is formed when a backslash \ is used.

Once the regex object, here regexobj, has been created the search method is used to search for the phone number pattern. The text is passed as argument to the search() method.

Finally a print statement is used to print out the matched pattern using the group() function.

See also 3 steps to reading text files in python


No Comment to " How to search for specific pattern in a text using Python "