diff options
author | Fred Drake <fdrake@acm.org> | 2002-03-16 05:10:22 (GMT) |
---|---|---|
committer | Fred Drake <fdrake@acm.org> | 2002-03-16 05:10:22 (GMT) |
commit | 4170544859e29b3a0fe9266d7809d4d7c25eae1a (patch) | |
tree | 9b90f20acf7feff0ee72316c7da26e2e1407e7fb /Doc | |
parent | c5e9264c993f1bca9e16d24747066efda161d845 (diff) | |
download | cpython-4170544859e29b3a0fe9266d7809d4d7c25eae1a.zip cpython-4170544859e29b3a0fe9266d7809d4d7c25eae1a.tar.gz cpython-4170544859e29b3a0fe9266d7809d4d7c25eae1a.tar.bz2 |
Clarify the descriptions of the positive and negative lookbehind assertions.
Added examples of positive lookbehind assertions.
This closes SF bug #529708.
Diffstat (limited to 'Doc')
-rw-r--r-- | Doc/lib/libre.tex | 39 |
1 files changed, 30 insertions, 9 deletions
diff --git a/Doc/lib/libre.tex b/Doc/lib/libre.tex index 1c14072..6df682c 100644 --- a/Doc/lib/libre.tex +++ b/Doc/lib/libre.tex @@ -272,18 +272,39 @@ followed by \code{'Asimov'}. \item[\code{(?<=...)}] Matches if the current position in the string is preceded by a match for \regexp{...} that ends at the current -position. This is called a positive lookbehind assertion. -\regexp{(?<=abc)def} will match \samp{abcdef}, since the lookbehind -will back up 3 characters and check if the contained pattern matches. -The contained pattern must only match strings of some fixed length, -meaning that \regexp{abc} or \regexp{a|b} are allowed, but \regexp{a*} -isn't. +position. This is called a \dfn{positive lookbehind assertion}. +\regexp{(?<=abc)def} will find a match in \samp{abcdef}, since the +lookbehind will back up 3 characters and check if the contained +pattern matches. The contained pattern must only match strings of +some fixed length, meaning that \regexp{abc} or \regexp{a|b} are +allowed, but \regexp{a*} and \regexp{a\{3,4\}} are not. Note that +patterns which start with positive lookbehind assertions will never +match at the beginning of the string being searched; you will most +likely want to use the \function{search()} function rather than the +\function{match()} function: + +\begin{verbatim} +>>> import re +>>> m = re.search('(?<=abc)def', 'abdef') +>>> m.group(0) +'def' +\end{verbatim} + +This example looks for a word following a hyphen: + +\begin{verbatim} +>>> m = re.search('(?<=-)\w+', 'spam-egg') +>>> m.group(0) +'egg' +\end{verbatim} \item[\code{(?<!...)}] Matches if the current position in the string -is not preceded by a match for \regexp{...}. This -is called a negative lookbehind assertion. Similar to positive lookbehind +is not preceded by a match for \regexp{...}. This is called a +\dfn{negative lookbehind assertion}. Similar to positive lookbehind assertions, the contained pattern must only match strings of some -fixed length. +fixed length. Patterns which start with negative lookbehind +assertions will may match at the beginning of the string being +searched. \end{list} |