Section 6.9. Related Modules


6.9. Related Modules

Table 6.10 lists the key related modules for strings that are part of the Python standard library.

Table 6.10. Related Modules for String Types

Module

Description

string

String manipulation and utility functions, i.e., Template class

re

Regular expressions: powerful string pattern matching

struct

Convert strings to/from binary data format

c/StringIO

String buffer object that behaves like a file

base64

Base 16, 32, and 64 data encoding and decoding

codecs

Codec registry and base classes

crypt

Performs one-way encryption cipher

difflib[a]

Various "differs" for sequences

hashlib[b]

API to many different secure hash and message digest algorithms

hmac[c]

Keyed-hashing for message authentication

md5[d]

RSA's MD5 message digest authentication

rotor

Provides multi-platform en/decryption services

sha[d]

NIST's secure hash algorithm SHA

stringprep[e]

Prepares Unicode strings for use in Internet protocols

textwrap[e]

Text-wrapping and filling

unicodedata

Unicode database


[a] New in Python 2.1.

[b] New in Python 2.5.

[c] New in Python 2.2.

[d] Obsoleted in Python 2.5 by hashlib module.

[e] New in Python 2.3.

Core Module: re

Regular expressions (REs) provide advanced pattern matching scheme for strings. Using a separate syntax that describes these patterns, you can effectively use them as "filters" when passing in the text to perform the searches on. These filters allow you to extract the matched patterns as well as perform find-and-replace or divide up strings based on the patterns that you describe.

The re module, introduced in Python 1.5, obsoletes the original regex and regsub modules from earlier releases. It represented a major upgrade in terms of Python's support for regular expressions, adopting the complete Perl syntax for REs. In Python 1.6, a completely new engine was written (SRE), which added support for Unicode strings as well as significant performance improvements. SRE replaces the old PCRE engine, which had been under the covers of the regular expression modules.

Some of the key functions in the re module include: compile() compiles an RE expression into a reusable RE object; match() attempts to match a pattern from the beginning of a string; search() searches for any matching pattern in the string; and sub() performs a search-and-replace of matches. Some of these functions return match objects with which you can access saved group matches (if any were found). All of Chapter 15 is dedicated to regular expressions.




Core Python Programming
Core Python Programming (2nd Edition)
ISBN: 0132269937
EAN: 2147483647
Year: 2004
Pages: 334
Authors: Wesley J Chun

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net