Khalsa Labs
regular expressions

Python Regular Expression Easy Tutorial

Regular Expressions provides the way to search desired keywords From a huge data, this technique is called Pattern Matching or text matching. Several Patterns for search text are build using Regular Expressions. e.g the Pattern for search digits only or words only etc.

Python has re module that use Perl like syntax for regular expressions.Here the simple tutorial let you learn Python Regular Expression Easy.

 

To understand regular expressions – remember two things TEXT and PATTERN.

TEXT = It is any text from which we want to search something.

PATTERN = It is the text or keywords which we want to search.

 

LESSON 1 : INTRODUCTION

 

TEXT =  “My Name is Harman”

PATTERN = “Harman”

It Will Search-  ‘Harman’ from the Text.

 

For Same thing in Python:

 

import re
txt = “my name is Harman”
patt = “Harman”
m = re.search(patt,txt)print m.group()

 

Explained:

Import re  ->  imports re module. re stands for Regular Expressions, module in python by default

re.search(patt,txt)  ->  searches the given pattern with given text.

m.group()  ->  gave the result groupwise, usually one result is produced if pattern matched.

 

TINY CODE:

import re
print re.search(“german”,”the german”).group()

 

Next Lesson will provide the use of Patterns for find text.

The following two tabs change content below.
Python Developer and DIY lover.

want to comment ??

Your email address will not be published. Required fields are marked *