Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit f275803

Browse files
committed
Clarify the descriptions of the positive and negative lookbehind assertions.
Added examples of positive lookbehind assertions. This closes SF bug #529708.
1 parent 0e4cd7f commit f275803

1 file changed

Lines changed: 30 additions & 9 deletions

File tree

Doc/lib/libre.tex

Lines changed: 30 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -272,18 +272,39 @@ \subsection{Regular Expression Syntax \label{re-syntax}}
272272

273273
\item[\code{(?<=...)}] Matches if the current position in the string
274274
is preceded by a match for \regexp{...} that ends at the current
275-
position. This is called a positive lookbehind assertion.
276-
\regexp{(?<=abc)def} will match \samp{abcdef}, since the lookbehind
277-
will back up 3 characters and check if the contained pattern matches.
278-
The contained pattern must only match strings of some fixed length,
279-
meaning that \regexp{abc} or \regexp{a|b} are allowed, but \regexp{a*}
280-
isn't.
275+
position. This is called a \dfn{positive lookbehind assertion}.
276+
\regexp{(?<=abc)def} will find a match in \samp{abcdef}, since the
277+
lookbehind will back up 3 characters and check if the contained
278+
pattern matches. The contained pattern must only match strings of
279+
some fixed length, meaning that \regexp{abc} or \regexp{a|b} are
280+
allowed, but \regexp{a*} and \regexp{a\{3,4\}} are not. Note that
281+
patterns which start with positive lookbehind assertions will never
282+
match at the beginning of the string being searched; you will most
283+
likely want to use the \function{search()} function rather than the
284+
\function{match()} function:
285+
286+
\begin{verbatim}
287+
>>> import re
288+
>>> m = re.search('(?<=abc)def', 'abdef')
289+
>>> m.group(0)
290+
'def'
291+
\end{verbatim}
292+
293+
This example looks for a word following a hyphen:
294+
295+
\begin{verbatim}
296+
>>> m = re.search('(?<=-)\w+', 'spam-egg')
297+
>>> m.group(0)
298+
'egg'
299+
\end{verbatim}
281300

282301
\item[\code{(?<!...)}] Matches if the current position in the string
283-
is not preceded by a match for \regexp{...}. This
284-
is called a negative lookbehind assertion. Similar to positive lookbehind
302+
is not preceded by a match for \regexp{...}. This is called a
303+
\dfn{negative lookbehind assertion}. Similar to positive lookbehind
285304
assertions, the contained pattern must only match strings of some
286-
fixed length.
305+
fixed length. Patterns which start with negative lookbehind
306+
assertions will may match at the beginning of the string being
307+
searched.
287308

288309
\end{list}
289310

0 commit comments

Comments
 (0)