Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
C
cpython
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
Analytics
Analytics
Repository
Value Stream
Wiki
Wiki
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Commits
Issue Boards
Open sidebar
Kirill Smelkov
cpython
Commits
61e707aa
Commit
61e707aa
authored
Mar 04, 2002
by
Skip Montanaro
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
add simple example of avoiding backtracking
parent
bb96d8c3
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
22 additions
and
6 deletions
+22
-6
Doc/lib/libre.tex
Doc/lib/libre.tex
+22
-6
No files found.
Doc/lib/libre.tex
View file @
61e707aa
...
...
@@ -802,10 +802,6 @@ The string passed to \function{match()} or \function{search()}.
\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()
}
.
...
...
@@ -851,7 +847,27 @@ you would use a \cfunction{scanf()} format like
The equivalent regular expression would be
\begin{verbatim}
([
^
\s
]+) - (
\d
+) errors, (
\d
+) warnings
(
\S
+) - (
\d
+) errors, (
\d
+) warnings
\end{verbatim}
\leftline
{
\strong
{
Avoiding backtracking
}}
If you create regular expressions that require the engine to perform a lot
of backtracking, you may encounter a RuntimeError exception with the message
\code
{
maximum recursion limit exceeded
}
. For example,
\begin{verbatim}
>>> s = "<" + "that's a very big string!"*1000 + ">"
>>> re.match('<.*?>', s)
Traceback (most recent call last):
File "<stdin>", line 1, in ?
File "/usr/local/lib/python2.3/sre.py", line 132, in match
return
_
compile(pattern, flags).match(string)
RuntimeError: maximum recursion limit exceeded
\end{verbatim}
%\end{list}
You can often restructure your regular expression to avoid backtracking.
The above regular expression can be recast as
\regexp
{
\textless
[
\textasciicircum
\textgreater
]*
\textgreater
}
. As a further
benefit, such regular expressions will run faster than their backtracking
equivalents.
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment