[Python-checkins] CVS: python/dist/src/Doc/lib libre.tex,1.71,1.72
Fred L. Drake
fdrake@users.sourceforge.net
Thu, 29 Nov 2001 00:45:24 -0800
Update of /cvsroot/python/python/dist/src/Doc/lib
In directory usw-pr-cvs1:/tmp/cvs-serv18154/lib
Modified Files:
libre.tex
Log Message:
New section of regular expression examples contributed by Skip Montanaro,
with some extensions and changes from me.
This closes SF patch #472825.
Index: libre.tex
===================================================================
RCS file: /cvsroot/python/python/dist/src/Doc/lib/libre.tex,v
retrieving revision 1.71
retrieving revision 1.72
diff -C2 -d -r1.71 -r1.72
*** libre.tex 2001/11/05 21:34:36 1.71
--- libre.tex 2001/11/29 08:45:22 1.72
***************
*** 793,794 ****
--- 793,850 ----
The string passed to \function{match()} or \function{search()}.
\end{memberdesc}
+
+ \subsection{Examples}
+
+ %\begin{list}{}{\leftmargin 0.7in \labelwidth 0.65in}
+
+ %\item[Simulating scanf]
+
+ \leftline{\strong{Simulating \cfunction{scanf()}}}
+
+ Python does not currently have an equivalent to \cfunction{scanf()}.
+ \ttindex{scanf()}
+ Regular expressions are generally more powerful, though also more
+ verbose, than \cfunction{scanf()} format strings. The table below
+ offers some more-or-less equivalent mappings between
+ \cfunction{scanf()} format tokens and regular expressions.
+
+ \begin{tableii}{l|l}{textrm}{\cfunction{scanf()} Token}{Regular Expression}
+ \lineii{\code{\%c}}
+ {\regexp{.}}
+ \lineii{\code{\%5c}}
+ {\regexp{.\{5\}}}
+ \lineii{\code{\%d}}
+ {\regexp{[-+]\e d+}}
+ \lineii{\code{\%e}, \code{\%E}, \code{\%f}, \code{\%g}}
+ {\regexp{[-+](\e d+(\e.\e d*)?|\e d*\e.\e d+)([eE]\e d+)?}}
+ \lineii{\code{\%i}}
+ {\regexp{[-+](0[xX][\e dA-Fa-f]+|0[0-7]*|\e d+)}}
+ \lineii{\code{\%o}}
+ {\regexp{0[0-7]*}}
+ \lineii{\code{\%s}}
+ {\regexp{[\textasciicircum\e s]+}}
+ \lineii{\code{\%u}}
+ {\regexp{\e d+}}
+ \lineii{\code{\%x}, \code{\%X}}
+ {\regexp{0[xX][\e dA-Fa-f]}}
+ \end{tableii}
+
+ To extract the filename and numbers from a string like
+
+ \begin{verbatim}
+ /usr/sbin/sendmail - 0 errors, 4 warnings
+ \end{verbatim}
+
+ you would use a \cfunction{scanf()} format like
+
+ \begin{verbatim}
+ %s - %d errors, %d warnings
+ \end{verbatim}
+
+ The equivalent regular expression would be
+
+ \begin{verbatim}
+ ([^\s]+) - (\d+) errors, (\d+) warnings
+ \end{verbatim}
+
+ %\end{list}