II-MSC-PYTHON-UNIT-V-NOTES
II-MSC-PYTHON-UNIT-V-NOTES
CONTENTS
UNIT - 5
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'.
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.
2
groups()-This method returns all matching subgroups in a tuple (empty if there weren't
any)
Example
#!/usr/bin/python
import re
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!!"
This function searches for first occurrence of RE pattern within string with optional flags.
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.
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
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!!"
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!!
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
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
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).
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.
Following table lists the regular expression syntax that is available in Python
3
. - Matches any single character except newline. Using m option allows it to match newline
as well.
12 a| b-Matches either a or b.
16
(?: re)-Groups regular expressions without remembering matched text.
19 (?#...)-Comment.
26 \S-Matches nonwhitespace.
28 \D-Matches nondigits.
33
\b-Matches word boundaries when outside brackets. Matches backspace (0x08) when
inside brackets.
37
\10-Matches nth grouped sub expression if it matched already. Otherwise refers to the octal
representation of a character code.
Literal characters
1 Python-Match "python".
Character classes
6 \d{3,5}-Match 3, 4, or 5 digits
5.8.4 Alternatives
5.8.5 Anchors
6 \brub\B-\B is nonword boundary: match "rub" in "rube" and "ruby" but not alone
3 R(?i:uby)-Same as above
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.
Release − Latest release – 2.1 version, commonly used release – 1.8, 1.6 version.
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
URL routing
Authentication
Template engine
PART-A QUESTIONS
PART-B QUESTIONS