@@ -282,6 +282,35 @@ \subsection{Regular Expression Syntax \label{re-syntax}}
282282\end {list }
283283
284284
285+ \subsection {Matching vs. Searching \label {matching-searching } }
286+ \sectionauthor {Fred L. Drake, Jr.}{
[email protected] }
287+
288+ \strong {XXX This section is still incomplete!}
289+
290+ Python offers two different primitive operations based on regular
291+ expressions: match and search. If you are accustomed to Perl's
292+ semantics, the search operation is what you're looking for. See the
293+ \function {search()} function and corresponding method of compiled
294+ regular expression objects.
295+
296+ Note that match may differ from search using a regular expression
297+ beginning with \character {\^ }: \character {\^ } matches only at the start
298+ of the string, or in \constant {MULTILINE} mode also immediately
299+ following a newline. "match" succeeds only if the pattern matches at
300+ the start of the string regardless of mode, or at the starting
301+ position given by the optional \var {pos} argument regardless of
302+ whether a newline precedes it.
303+
304+ % Examples from Tim Peters:
305+ \begin {verbatim }
306+ re.compile("a").match("ba", 1) # succeeds
307+ re.compile("^a").search("ba", 1) # fails; 'a' not at start
308+ re.compile("^a").search("\na", 1) # fails; 'a' not at start
309+ re.compile("^a", re.M).search("\na", 1) # succeeds
310+ re.compile("^a", re.M).search("ba", 1) # fails; no preceding \n
311+ \end {verbatim }
312+
313+
285314\subsection {Module Contents }
286315\nodename {Contents of Module re}
287316
@@ -376,6 +405,9 @@ \subsection{Module Contents}
376405 \class {MatchObject} instance. Return \code {None} if the string does not
377406 match the pattern; note that this is different from a zero-length
378407 match.
408+
409+ \strong {Note:} If you want to locate a match anywhere in
410+ \var {string}, use \method {search()} instead.
379411\end {funcdesc }
380412
381413\begin {funcdesc }{split}{pattern, string, \optional {, maxsplit\code { = 0}}}
@@ -387,7 +419,7 @@ \subsection{Module Contents}
387419 element of the list. (Incompatibility note: in the original Python
388420 1.5 release, \var {maxsplit} was ignored. This has been fixed in
389421 later releases.)
390- %
422+
391423\begin {verbatim }
392424>>> re.split('\W+', 'Words, words, words.')
393425['Words', 'words', 'words', '']
@@ -396,7 +428,7 @@ \subsection{Module Contents}
396428>>> re.split('\W+', 'Words, words, words.', 1)
397429['Words', 'words, words.']
398430\end {verbatim }
399- %
431+
400432 This function combines and extends the functionality of
401433 the old \function {regsub.split()} and \function {regsub.splitx()}.
402434\end {funcdesc }
@@ -417,15 +449,15 @@ \subsection{Module Contents}
417449it is called for every non-overlapping occurance of \var {pattern}.
418450The function takes a single match object argument, and returns the
419451replacement string. For example:
420- %
452+
421453\begin {verbatim }
422454>>> def dashrepl(matchobj):
423455.... if matchobj.group(0) == '-': return ' '
424456.... else: return '-'
425457>>> re.sub('-{1,2}', dashrepl, 'pro----gram-files')
426458'pro--gram files'
427459\end {verbatim }
428- %
460+
429461The pattern may be a string or a
430462regex object; if you need to specify
431463regular expression flags, you must use a regex object, or use
@@ -498,7 +530,10 @@ \subsection{Regular Expression Objects \label{re-objects}}
498530 \class {MatchObject} instance. Return \code {None} if the string does not
499531 match the pattern; note that this is different from a zero-length
500532 match.
501-
533+
534+ \strong {Note:} If you want to locate a match anywhere in
535+ \var {string}, use \method {search()} instead.
536+
502537 The optional second parameter \var {pos} gives an index in the string
503538 where the search is to start; it defaults to \code {0}. This is not
504539 completely equivalent to slicing the string; the \code {'\^ '} pattern
0 commit comments