[go: up one dir, main page]

0% found this document useful (0 votes)
2 views18 pages

II-MSC-PYTHON-UNIT-V-NOTES

The document outlines Unit 5 of a programming course using Python, focusing on regular expressions and their applications. It covers key functions such as match and search, along with modifiers, patterns, and examples of usage. Additionally, it discusses special character classes, repetition cases, and concepts like grouping and back references.

Uploaded by

RCW CS 18
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views18 pages

II-MSC-PYTHON-UNIT-V-NOTES

The document outlines Unit 5 of a programming course using Python, focusing on regular expressions and their applications. It covers key functions such as match and search, along with modifiers, patterns, and examples of usage. Additionally, it discusses special character classes, repetition cases, and concepts like grouping and back references.

Uploaded by

RCW CS 18
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 18

DCS33– PROGRAMMING USING PYTHON

CONTENTS
UNIT - 5

Chapter Topic’s Page No


5.1 Regular Expression 1
5.2 The match Function 1
5.3 The search Function 3
5.3.1 Matching Versus Searching 4
5.4 Search and Replace 5
5.5 Regular Expression Modifiers: Option Flags 6
5.6 Regular Expression Patterns 7
5.6.1 Regular Expression Examples 10
5.7 Special Character Classes 11
5.8 Repetition Cases 12
5.8.1 Non_greedy repetition 12
5.8.2 Grouping with Parentheses 13
5.8.3 Back references 13
5.8.4 Alternatives 14
5.8.5 Anchors 14
5.8.6 Special Syntax with Parentheses 15
5.9 Web development framework 15
5.9.1 Django Architecture 15
DCS33-PROGRAMMING USING PYTHON UNIT V

5.1 Regular Expression

 A regular expression is a special sequence of characters that helps you match or find other
strings or sets of strings, using a specialized syntax held in a pattern.

 Regular expressions are widely used in UNIX world. The Python module re provides
full support for Perl-like regular expressions in Python.

 The re module raises the exception re.error if an error occurs while compiling or using a
regular expression.

 We would cover two important functions, which would be used to handle regular
expressions.

 But a small thing first: There are various characters, which would have special meaning
when they are used in regular expression. To avoid any confusion while dealing with
regular expressions, we would use Raw Strings as r'expression'.

5.2 The match Function

 This function attempts to match RE pattern to string with optional flags.

 Here is the syntax for this function − re.match(pattern, string, flags=0)

S.No. Parameter & Description

1 Pattern-This is the regular expression to be matched.

2
String-This is the string, which would be searched to match the pattern at the beginning of
string.

3 Flags-You can specify different flags using bitwise OR (|). These are modifiers, which are
listed in the table below.

1 DEPARTMENT OF COMPUTER SCIENCE-RCASW-MS.K.RATHI DEVI


DCS33-PROGRAMMING USING PYTHON UNIT V

 The re.match function returns a match object on success, None on failure.

 We usegroup(num) or groups() function of match object to get matched expression.

S.No. Match Object Method & Description

1 group(num=0)-This method returns entire match (or specific subgroup num)

2
groups()-This method returns all matching subgroups in a tuple (empty if there weren't
any)

Example
#!/usr/bin/python
import re

line = "Cats are smarter than dogs"

matchObj = re.match( r'(.*) are (.*?) .*', line, re.M|re.I)

if matchObj:
print "matchObj.group() : ", matchObj.group()
print "matchObj.group(1) : ", matchObj.group(1)
print "matchObj.group(2) : ", matchObj.group(2)
else:
print "No match!!"

When the above code is executed, it produces following result −

matchObj.group() : Cats are smarter than dogs


matchObj.group(1) : Cats
matchObj.group(2) : smarter

2 DEPARTMENT OF COMPUTER SCIENCE-RCASW-MS.K.RATHI DEVI


DCS33-PROGRAMMING USING PYTHON UNIT V

5.3 The search Function

 This function searches for first occurrence of RE pattern within string with optional flags.

 Here is the syntax for this function − re.search(pattern, string, flags=0)

S.No. Parameter & Description

1 Pattern-This is the regular expression to be matched.

2
String-This is the string, which would be searched to match the pattern anywhere in the
string.

3
Flags-You can specify different flags using bitwise OR (|). These are modifiers, which are
listed in the table below.

 The re.search function returns a match object on success, none on failure.

 We use group(num) or groups() function of match object to get matched expression.

S.No. Match Object Methods & Description

1 group(num=0)-This method returns entire match (or specific subgroup num)

2 groups()-This method returns all matching subgroups in a tuple (empty if there weren't
any)

Example
#!/usr/bin/python
import re
line = "Cats are smarter than dogs";
3 DEPARTMENT OF COMPUTER SCIENCE-RCASW-MS.K.RATHI DEVI
DCS33-PROGRAMMING USING PYTHON UNIT V

searchObj = re.search( r'(.*) are (.*?) .*', line, re.M|re.I)


if searchObj:
print "searchObj.group() : ", searchObj.group()
print "searchObj.group(1) : ", searchObj.group(1)
print "searchObj.group(2) : ", searchObj.group(2)
else:
print "Nothing found!!"

When the above code is executed, it produces following result −

searchObj.group() : Cats are smarter than dogs


searchObj.group(1) : Cats
searchObj.group(2) : smarter

5.3.1 Matching Versus Searching

 Python offers two different primitive operations based on regular expressions:

 match checks for a match only at the beginning of the string, while

 Search checks for a match anywhere in the string (this is what Perl does by
default).

Example
#!/usr/bin/python
import re
line = "Cats are smarter than dogs";
matchObj = re.match( r'dogs', line, re.M|re.I)
if matchObj:
print "match --> matchObj.group() : ", matchObj.group()
else:
print "No match!!"

searchObj = re.search( r'dogs', line, re.M|re.I)

4 DEPARTMENT OF COMPUTER SCIENCE-RCASW-MS.K.RATHI DEVI


DCS33-PROGRAMMING USING PYTHON UNIT V

if searchObj:
print "search --> searchObj.group() : ", searchObj.group()
else:
print "Nothing found!!"

When the above code is executed, it produces the following result − No match!!

search --> searchObj.group() : dogs

5.4 Search and Replace

 One of the most important re methods that use regular expressions is sub.

Syntax
re.sub(pattern, repl, string, max=0)

This method replaces all occurrences of the RE pattern in string with repl, substituting
all occurrences unless max provided. This method returns modified string.

Example
#!/usr/bin/python
import re

phone = "2004-959-559 # This is Phone Number"

# Delete Python-style comments


num = re.sub(r'#.*$', "", phone)
print "Phone Num : ", num

# Remove anything other than digits


num = re.sub(r'\D', "", phone)
print "Phone Num : ", num

When the above code is executed, it produces the following result −

DEPARTMENT OF COMPUTER SCIENCE-RCASW-MS.K.RATHI DEVI


5
DCS33-PROGRAMMING USING PYTHON UNIT V

Phone Num : 2004-959-559


Phone Num : 2004959559

5.5 Regular Expression Modifiers: Option Flags

 Regular expression literals may include an optional modifier to control various aspects
of matching.

 The modifiers are specified as an optional flag. You can provide multiple modifiers
using exclusive OR (|), as shown previously and may be represented by one of these

S.No. Modifier & Description

1 re.I-Performs case-insensitive matching.

2
re.L-Interprets words according to the current locale. This interpretation affects the
alphabetic group (\w and \W), as well as word boundary behavior(\b and \B).

3 re.M-Makes $ match the end of a line (not just the end of the string) and makes ^ match
the start of any line (not just the start of the string).

4 re.S-Makes a period (dot) match any character, including a newline.

5
re.U-Interprets letters according to the Unicode character set. This flag affects the behavior
of \w, \W, \b, \B.

6
re.X-Permits "cuter" regular expression syntax. It ignores whitespace (except inside a set []
or when escaped by a backslash) and treats unescaped # as a comment marker.

6 DEPARTMENT OF COMPUTER SCIENCE-RCASW-MS.K.RATHI DEVI


DCS33-PROGRAMMING USING PYTHON UNIT V

5.6 Regular Expression Patterns

 Except for control characters, (+ ? . * ^ $ ( ) [ ] { } | \), all characters match themselves.


You can escape a control character by preceding it with a backslash.

Following table lists the regular expression syntax that is available in Python

S.No. Pattern & Description

1 ^- Matches beginning of line.

2 $-Matches end of line.

3
. - Matches any single character except newline. Using m option allows it to match newline
as well.

4 [...]-Matches any single character in brackets.

5 [^...]-Matches any single character not in brackets

6 re*-Matches 0 or more occurrences of preceding expression.

7 re+-Matches 1 or more occurrence of preceding expression.

8 re?-Matches 0 or 1 occurrence of preceding expression.

9 re{ n}-Matches exactly n number of occurrences of preceding expression.

7 DEPARTMENT OF COMPUTER SCIENCE-RCASW-MS.K.RATHI DEVI


DCS33-PROGRAMMING USING PYTHON UNIT V

10 re{ n,}-Matches n or more occurrences of preceding expression.

11 re{ n, m}-Matches at least n and at most m occurrences of preceding expression.

12 a| b-Matches either a or b.

13 (re)-Groups regular expressions and remembers matched text.

14 (?imx)-Temporarily toggles on i, m, or x options within a regular expression. If in


parentheses, only that area is affected.

15 (?-imx)-Temporarily toggles off i, m, or x options within a regular expression. If in


parentheses, only that area is affected.

16
(?: re)-Groups regular expressions without remembering matched text.

17 (?imx: re)-Temporarily toggles on i, m, or x options within parentheses.

18 (?-imx: re)-Temporarily toggles off i, m, or x options within parentheses.

19 (?#...)-Comment.

20 (?= re)-Specifies position using a pattern. Doesn't have a range.

21 (?! re)-Specifies position using pattern negation. Doesn't have a range.

8 DEPARTMENT OF COMPUTER SCIENCE-RCASW-MS.K.RATHI DEVI


DCS33-PROGRAMMING USING PYTHON UNIT V

22 (?> re)-Matches independent pattern without backtracking.

23 \w-Matches word characters.

24 \W-Matches nonword characters.

25 \s-Matches whitespace. Equivalent to [\t\n\r\f].

26 \S-Matches nonwhitespace.

27 \d-Matches digits. Equivalent to [0-9].

28 \D-Matches nondigits.

29 \A-Matches beginning of string.

30 \Z-Matches end of string. If a newline exists, it matches just before newline.

31 \z-Matches end of string.

32 \G-Matches point where last match finished.

33
\b-Matches word boundaries when outside brackets. Matches backspace (0x08) when
inside brackets.

34 \B-Matches non word boundaries.

9 DEPARTMENT OF COMPUTER SCIENCE-RCASW-MS.K.RATHI DEVI


DCS33-PROGRAMMING USING PYTHON UNIT V

35 \n, \t, etc.-Matches newlines, carriage returns, tabs, etc.

36 \1...\9-Matches nth grouped sub expression.

37
\10-Matches nth grouped sub expression if it matched already. Otherwise refers to the octal
representation of a character code.

5.6.1 Regular Expression Examples

Literal characters

S.No. Example & Description

1 Python-Match "python".

Character classes

S.No. Example & Description

1 [Pp]ython-Match "Python" or "python"

2 rub[ye]-Match "ruby" or "rube"

3 [aeiou]-Match any one lowercase vowel

4 [0-9]-Match any digit; same as [0123456789]

10 DEPARTMENT OF COMPUTER SCIENCE-RCASW-MS.K.RATHI DEVI


DCS33-PROGRAMMING USING PYTHON UNIT V

5 [a-z]-Match any lowercase ASCII letter

6 [A-Z]-Match any uppercase ASCII letter

7 [a-zA-Z0-9]-Match any of the above

8 [^aeiou]-Match anything other than a lowercase vowel

9 [^0-9]-Match anything other than a digit

5.7 Special Character Classes

S.No. Example & Description

1 .-Match any character except newline

2 \d-Match a digit: [0-9]

3 \D-Match a non digit: [^0-9]

4 \s-Match a whitespace character: [ \t\r\n\f]

5 \S-Match non white space: [^ \t\r\n\f]

6 \w-Match a single word character: [A-Za-z0-9_]

11 DEPARTMENT OF COMPUTER SCIENCE-RCASW-MS.K.RATHI DEVI


DCS33-PROGRAMMING USING PYTHON UNIT V

7 \W-Match a non word character: [^A-Za-z0-9_]

5.8 Repetition Cases

S.No. Example & Description

1 ruby?-Match "rub" or "ruby": the y is optional

2 ruby*-Match "rub" plus 0 or more ys

3 ruby+-Match "rub" plus 1 or more ys

4 \d{3}-Match exactly 3 digits

5 \d{3,}-Match 3 or more digits

6 \d{3,5}-Match 3, 4, or 5 digits

5.8.1 Non_greedy repetition

This matches the smallest number of repetitions −

S.No. Example & Description

1 <.*>-Greedy repetition: matches "<python>perl>"

12 DEPARTMENT OF COMPUTER SCIENCE-RCASW-MS.K.RATHI DEVI


DCS33-PROGRAMMING USING PYTHON UNIT V

2 <.*?>-Nongreedy: matches "<python>" in "<python>perl>"

5.8.2 Grouping with Parentheses

S.No. Example & Description

1 \D\d+-No group: + repeats \d

2 (\D\d)+-Grouped: + repeats \D\d pair

3 ([Pp]ython(, )?)+-Match "Python", "Python, python, python", etc.

5.8.3 Back references

This matches a previously matched group again −

S.No. Example & Description

1 ([Pp])ython&\1ails-Match python&pails or Python&Pails

2 (['"])[^\1]*\1-Single or double-quoted string. \1 matches whatever the 1st group matched.


\2 matches whatever the 2nd group matched, etc.

13 DEPARTMENT OF COMPUTER SCIENCE-RCASW-MS.K.RATHI DEVI


DCS33-PROGRAMMING USING PYTHON UNIT V

5.8.4 Alternatives

S.No. Example & Description

1 python|perl-Match "python" or "perl"

2 rub(y|le))-Match "ruby" or "ruble"

3 Python(!+|\?)-"Python" followed by one or more ! or one ?

5.8.5 Anchors

This needs to specify match position.

S.No. Example & Description

1 ^Python-Match "Python" at the start of a string or internal line

2 Python$-Match "Python" at the end of a string or line

3 \APython-Match "Python" at the start of a string

4 Python\Z-Match "Python" at the end of a string

5 \bPython\b-Match "Python" at a word boundary

6 \brub\B-\B is nonword boundary: match "rub" in "rube" and "ruby" but not alone

14 DEPARTMENT OF COMPUTER SCIENCE-RCASW-MS.K.RATHI DEVI


DCS33-PROGRAMMING USING PYTHON UNIT V

7 Python(?=!)-Match "Python", if followed by an exclamation point.

8 Python(?!!)-Match "Python", if not followed by an exclamation point.

5.8.6 Special Syntax with Parentheses

S.No. Example & Description

1 R(?#comment)-Matches "R". All the rest is a comment

2 R(?i)uby-Case-insensitive while matching "uby"

3 R(?i:uby)-Same as above

4 rub(?:y|le))-Group only without creating \1 backreference

5.9 Web development framework

 Python is one of the most acceptable languages among web and application developers
because of its strong emphasis on efficiency and readability.

 There are numerous outstanding Python web frameworks, each with their own
specialties and features.

5.9.1 Django Architecture

Category − Django belongs to the full-stack Python framework.

Release − Latest release – 2.1 version, commonly used release – 1.8, 1.6 version.

15 DEPARTMENT OF COMPUTER SCIENCE-RCASW-MS.K.RATHI DEVI


DCS33-PROGRAMMING USING PYTHON UNIT V

About

 Built by experienced developers, Django is a high level Python web framework which
allows rapid, clean and pragmatic design development.

 Django handles much of the complexities of web development, so you can focus on
writing your app without a need to reinvent the wheel. It’s free and open source.

 To map objects to database table, Django uses ORM and the same is used to transfer
from one database to other.

 It works with mostly all important databases like Oracle, MySQL, PostgreSQL, SQLite,
etc.

 There are numerous websites in the industry which uses Django as their primary
framework for backend development.

Features of Django

Some of the exemplary features of this Python web framework are

 URL routing

 Authentication

 Database schema migrations

 ORM (Object-relational mapper)

 Template engine

DEPARTMENT OF COMPUTER SCIENCE-RCASW-MS.K.RATHI DEVI


16
DCS33-PROGRAMMING USING PYTHON UNIT V

REGULAR EXPRESSIONS & WEB APPLICATIONS

Syllabus: [Regulation: 2021]


UNIT - V: Regular Expressions: The match() function - The search() function - Search and Replace
-Regular Expression Modifiers: Option Flags - Regular Expression Patterns – Character Classes -
Special Character Classes - Repetition Cases - findall() method - compile() method. Web Application
Framework- Django Architecture- Starting development- Case Study: Blogging App.

PART-A QUESTIONS

1. What is regular expression?


2. Define option flags.
3. What is a repetition case?
4. What do you mean by modifier?
5. What do you mean by wild cart character?
6. Why Use Regular Expression?
7. What is Non_greedy repetition?

PART-B QUESTIONS

1. Describe any one regular expression with an example?


2. What is matching? Explain with example program.
3. What is searching? Explain with example program.
4. Explain math() function with suitable example?
5. Explain search and replace in regular expression.
PART-C QUESTIONS
1. What is regular expression? Explain various patterns of regular expression.
2. Explain replace () and split () methods of regular expression with suitable examples.
3. What is the difference between Django, Pyramid, and Flask?

1 DEPARTMENT OF COMPUTER SCIENCE-RCASW-MS.K.RATHI DEVI

You might also like