home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
The Datafile PD-CD 5
/
DATAFILE_PDCD5.iso
/
utilities
/
p
/
python
/
pyhtmldoc
/
r
/
regsub
< prev
next >
Wrap
Text File
|
1996-11-14
|
3KB
|
49 lines
<TITLE>regsub -- Python library reference</TITLE>
Next: <A HREF="../s/struct" TYPE="Next">struct</A>
Prev: <A HREF="../r/regex" TYPE="Prev">regex</A>
Up: <A HREF="../s/string_services" TYPE="Up">String Services</A>
Top: <A HREF="../t/top" TYPE="Top">Top</A>
<H1>4.3. Standard Module <CODE>regsub</CODE></H1>
This module defines a number of functions useful for working with
regular expressions (see built-in module <CODE>regex</CODE>).
<P>
Warning: these functions are not thread-safe.
<P>
<DL><DT><B>sub</B> (<VAR>pat</VAR>, <VAR>repl</VAR>, <VAR>str</VAR>) -- function of module regsub<DD>
Replace the first occurrence of pattern <VAR>pat</VAR> in string
<VAR>str</VAR> by replacement <VAR>repl</VAR>. If the pattern isn't found,
the string is returned unchanged. The pattern may be a string or an
already compiled pattern. The replacement may contain references
`<SAMP>\<VAR>digit</VAR></SAMP>' to subpatterns and escaped backslashes.
</DL>
<DL><DT><B>gsub</B> (<VAR>pat</VAR>, <VAR>repl</VAR>, <VAR>str</VAR>) -- function of module regsub<DD>
Replace all (non-overlapping) occurrences of pattern <VAR>pat</VAR> in
string <VAR>str</VAR> by replacement <VAR>repl</VAR>. The same rules as for
<CODE>sub()</CODE> apply. Empty matches for the pattern are replaced only
when not adjacent to a previous match, so e.g.
<CODE>gsub('', '-', 'abc')</CODE> returns <CODE>'-a-b-c-'</CODE>.
</DL>
<DL><DT><B>split</B> (<VAR>str</VAR>, <VAR>pat</VAR>[, <VAR>maxsplit</VAR>]) -- function of module regsub<DD>
Split the string <VAR>str</VAR> in fields separated by delimiters matching
the pattern <VAR>pat</VAR>, and return a list containing the fields. Only
non-empty matches for the pattern are considered, so e.g.
<CODE>split('a:b', ':*')</CODE> returns <CODE>['a', 'b']</CODE> and
<CODE>split('abc', '')</CODE> returns <CODE>['abc']</CODE>. The <VAR>maxsplit</VAR>
defaults to 0. If it is nonzero, only <VAR>maxsplit</VAR> number of splits
occur, and the remainder of the string is returned as the final
element of the list.
</DL>
<DL><DT><B>splitx</B> (<VAR>str</VAR>, <VAR>pat</VAR>[, <VAR>maxsplit</VAR>]) -- function of module regsub<DD>
Split the string <VAR>str</VAR> in fields separated by delimiters matching
the pattern <VAR>pat</VAR>, and return a list containing the fields as well
as the separators. For example, <CODE>splitx('a:::b', ':*')</CODE> returns
<CODE>['a', ':::', 'b']</CODE>. Otherwise, this function behaves the same
as <CODE>split</CODE>.
</DL>
<DL><DT><B>capwords</B> (<VAR>s</VAR>[, <VAR>pat</VAR>]) -- function of module regsub<DD>
Capitalize words separated by optional pattern <VAR>pat</VAR>. The default
pattern uses any characters except letters, digits and underscores as
word delimiters. Capitalization is done by changing the first
character of each word to upper case.
</DL>