summaryrefslogtreecommitdiffstats
path: root/Doc/lib
diff options
context:
space:
mode:
authorFred Drake <fdrake@acm.org>2001-11-29 08:45:22 (GMT)
committerFred Drake <fdrake@acm.org>2001-11-29 08:45:22 (GMT)
commit1cec7fab1dfd0956343c900d19cf1a7683abce75 (patch)
treec561e534e40b076713d926c4310cb90ef14fca05 /Doc/lib
parent92024d1a99dff25d5a1e53e39292cbfb30d2a226 (diff)
downloadcpython-1cec7fab1dfd0956343c900d19cf1a7683abce75.zip
cpython-1cec7fab1dfd0956343c900d19cf1a7683abce75.tar.gz
cpython-1cec7fab1dfd0956343c900d19cf1a7683abce75.tar.bz2
New section of regular expression examples contributed by Skip Montanaro,
with some extensions and changes from me. This closes SF patch #472825.
Diffstat (limited to 'Doc/lib')
-rw-r--r--Doc/lib/libre.tex56
1 files changed, 56 insertions, 0 deletions
diff --git a/Doc/lib/libre.tex b/Doc/lib/libre.tex
index 7100753..dc4ca2e 100644
--- a/Doc/lib/libre.tex
+++ b/Doc/lib/libre.tex
@@ -792,3 +792,59 @@ The regular expression object whose \method{match()} or
\begin{memberdesc}[MatchObject]{string}
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}