Python Tutorial ( PDFDrive )
CHAPTER19Python Regular ExpressionsAregular expression is a special sequence of characters that helps you match or find other strings or setsof strings, using a specialized syntax held in a pattern. Regular expressions are widely used in UNIX world.The module re provides full support for Perl-like regular expressions in Python. The re module raises theexception 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 thingfirst: 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'.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)Here is the description of the parameters:ParameterPatternStringFlagsDescriptionThis is the regular expression to be matched.This is the string, which would be searched to match the pattern at the beginningof string.You can specify different flags using bitwise OR (|). These are modifiers, whichare listed in the table below.The re.match function returns a match object on success, None on failure. We would use group(num)or groups() function of match object to get matched expression.Match Object Methodsgroup(num=0)DescriptionThis method returns entire match (or specific subgroup num)TUTORIALS POINT Simply Easy Learning
groups()This method returns all matching subgroups in a tuple (empty if there weren't any)EXAMPLE: #!/usr/bin/pythonimport reline = "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 the following result:matchObj.group(): Cats arematchObj.group(1) : CatsmatchObj.group(2) : smarterThe 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)Here is the description of the parameters:ParameterpatternstringflagsDescriptionThis is the regular expression to be matched.This is the string, which would be searched to match the pattern anywhere in thestring.You can specify different flags using bitwise OR (|). These are modifiers, whichare listed in the table below.The re.search function returns a match object on success, None on failure. We woulduse group(num) or groups() function of match object to get matched expression.Match Object Methodsgroup(num=0)groups()DescriptionThis method returns entire match (or specific subgroup num)This method returns all matching subgroups in a tuple (empty if there weren'tany)TUTORIALS POINT Simply Easy Learning
- Page 267 and 268: path = "/var/www/html/foo.txt"fd =
- Page 269 and 270: Example The following example shows
- Page 271 and 272: _vti_txtrobots.txtitemlistingresume
- Page 273 and 274: # Close opened fileos.close( fd )#
- Page 275 and 276: dev_num = os.makedev(major_dnum, mi
- Page 277 and 278: Return Value This method does not
- Page 279 and 280: -rw-------. 1 root root 0 Apr 30 02
- Page 281 and 282: os.pathconf(path, name) Descriptio
- Page 283 and 284: Description The method popen() open
- Page 285 and 286: # !/usr/bin/pythonimport ossrc = '/
- Page 287 and 288: Syntax Following is the syntax for
- Page 289 and 290: Return Value This method does not
- Page 291 and 292: # stat informationstatinfo = os.sta
- Page 293 and 294: Description The method tcgetpgrp()
- Page 295 and 296: os.tempnam(dir, prefix)Parameters
- Page 297 and 298: Description The method ttyname() re
- Page 299 and 300: Return Value This method does not
- Page 301 and 302: Return Value This method returns t
- Page 303 and 304: KeyErrorNameErrorUnboundLocalErrorE
- Page 305 and 306: ......................else:If there
- Page 307 and 308: print "Error: can\'t find file or r
- Page 309 and 310: self.args = argSo once you defined
- Page 311 and 312: 'Optional class documentation strin
- Page 313 and 314: For the above class let's try to ac
- Page 315 and 316: print 'Calling child method'c = Chi
- Page 317: Data Hiding: An object's attribute
- Page 321 and 322: SYNTAX: re.sub(pattern, repl, strin
- Page 323 and 324: \s Matches whitespace. Equivalent t
- Page 325 and 326: Backreferences: This matches a prev
- Page 327 and 328: CHAPTER20Python CGI ProgrammingWhat
- Page 329 and 330: If you click hello.py, then this pr
- Page 331 and 332: Simple URL Example : Get Metho
- Page 333 and 334: </form>The result of this code is t
- Page 335 and 336: text_content = form.getvalue('textc
- Page 337 and 338: Here is an example of how to retrie
- Page 339 and 340: CHAPTER21Python Database AccessThe
- Page 341 and 342: EXAMPLE: Following is the example o
- Page 343 and 344: ('Mac', 'Mohan', 20, 'M', 2000)try:
- Page 345 and 346: DELETE Operation: DELETE operation
- Page 347 and 348: ProgrammingErrorSubclass of Databas
- Page 349 and 350: The socket Module: To create a so
- Page 351 and 352: Protocol Common function Port No Py
- Page 353 and 354: Subject: SMTP e-mail testThis is a
- Page 355 and 356: try:smtpObj = smtplib.SMTP('localho
- Page 357 and 358: count = 0while count < 5:time.sleep
- Page 359 and 360: Synchronizing Threads: The threadi
- Page 361 and 362: thread = myThread(threadID, tName,
- Page 363 and 364: <stars>10</stars><description>Talk
- Page 365 and 366: print "Rating:", self.ratingelif se
- Page 367 and 368: Format: DVDRating: PGDescription: T
CHAPTER
19
Python Regular Expressions
Aregular 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 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'.
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)
Here is the description of the parameters:
Parameter
Pattern
String
Flags
Description
This is the regular expression to be matched.
This is the string, which would be searched to match the pattern at the beginning
of string.
You can specify different flags using bitwise OR (|). These are modifiers, which
are listed in the table below.
The re.match function returns a match object on success, None on failure. We would use group(num)
or groups() function of match object to get matched expression.
Match Object Methods
group(num=0)
Description
This method returns entire match (or specific subgroup num)
TUTORIALS POINT
Simply Easy Learning