Skip to content
Projeler
Gruplar
Parçacıklar
Yardım
Yükleniyor...
Oturum aç / Kaydol
Gezinmeyi değiştir
C
cpython
Proje
Proje
Ayrıntılar
Etkinlik
Cycle Analytics
Depo (repository)
Depo (repository)
Dosyalar
Kayıtlar (commit)
Dallar (branch)
Etiketler
Katkıda bulunanlar
Grafik
Karşılaştır
Grafikler
Konular (issue)
0
Konular (issue)
0
Liste
Pano
Etiketler
Kilometre Taşları
Birleştirme (merge) Talepleri
0
Birleştirme (merge) Talepleri
0
CI / CD
CI / CD
İş akışları (pipeline)
İşler
Zamanlamalar
Grafikler
Paketler
Paketler
Wiki
Wiki
Parçacıklar
Parçacıklar
Üyeler
Üyeler
Collapse sidebar
Close sidebar
Etkinlik
Grafik
Grafikler
Yeni bir konu (issue) oluştur
İşler
Kayıtlar (commit)
Konu (issue) Panoları
Kenar çubuğunu aç
Batuhan Osman TASKAYA
cpython
Commits
d9de93eb
Kaydet (Commit)
d9de93eb
authored
Şub 29, 2012
tarafından
Ezio Melotti
Dosyalara gözat
Seçenekler
Dosyalara Gözat
İndir
Eposta Yamaları
Sade Fark
#14155: remove duplication about search vs match in re doc.
üst
38ae5b23
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
28 additions
and
52 deletions
+28
-52
re.rst
Doc/library/re.rst
+28
-52
No files found.
Doc/library/re.rst
Dosyayı görüntüle @
d9de93eb
...
@@ -400,31 +400,6 @@ a group reference. As for string literals, octal escapes are always at most
...
@@ -400,31 +400,6 @@ a group reference. As for string literals, octal escapes are always at most
three digits in length.
three digits in length.
.. _matching-searching:
Matching vs Searching
---------------------
.. sectionauthor:: Fred L. Drake, Jr. <fdrake@acm.org>
Python offers two different primitive operations based on regular expressions:
**match** checks for a match only at the beginning of the string, while
**search** checks for a match anywhere in the string (this is what Perl does
by default).
Note that match may differ from search even when using a regular expression
beginning with ``'^'``: ``'^'`` matches only at the start of the string, or in
:const:`MULTILINE` mode also immediately following a newline. The "match"
operation succeeds only if the pattern matches at the start of the string
regardless of mode, or at the starting position given by the optional *pos*
argument regardless of whether a newline precedes it.
>>> re.match("c", "abcdef") # No match
>>> re.search("c", "abcdef") # Match
<_sre.SRE_Match object at ...>
.. _contents-of-module-re:
.. _contents-of-module-re:
Module Contents
Module Contents
...
@@ -547,10 +522,11 @@ form.
...
@@ -547,10 +522,11 @@ form.
Return ``None`` if the string does not match the pattern; note that this is
Return ``None`` if the string does not match the pattern; note that this is
different from a zero-length match.
different from a zero-length match.
.. note::
Note that even in :const:`MULTILINE` mode, :func:`re.match` will only match
at the beginning of the string and not at the beginning of each line.
If you want to locate a match anywhere in *string*, use :func:`search`
If you want to locate a match anywhere in *string*, use :func:`search`
instead
.
instead (see also :ref:`search-vs-match`)
.
.. function:: split(pattern, string, maxsplit=0, flags=0)
.. function:: split(pattern, string, maxsplit=0, flags=0)
...
@@ -746,16 +722,14 @@ Regular Expression Objects
...
@@ -746,16 +722,14 @@ Regular Expression Objects
The optional *pos* and *endpos* parameters have the same meaning as for the
The optional *pos* and *endpos* parameters have the same meaning as for the
:meth:`~RegexObject.search` method.
:meth:`~RegexObject.search` method.
.. note::
If you want to locate a match anywhere in *string*, use
:meth:`~RegexObject.search` instead.
>>> pattern = re.compile("o")
>>> pattern = re.compile("o")
>>> pattern.match("dog") # No match as "o" is not at the start of "dog".
>>> pattern.match("dog") # No match as "o" is not at the start of "dog".
>>> pattern.match("dog", 1) # Match as "o" is the 2nd character of "dog".
>>> pattern.match("dog", 1) # Match as "o" is the 2nd character of "dog".
<_sre.SRE_Match object at ...>
<_sre.SRE_Match object at ...>
If you want to locate a match anywhere in *string*, use
:meth:`~RegexObject.search` instead (see also :ref:`search-vs-match`).
.. method:: RegexObject.split(string, maxsplit=0)
.. method:: RegexObject.split(string, maxsplit=0)
...
@@ -1121,37 +1095,39 @@ avoid recursion. Thus, the above regular expression can avoid recursion by
...
@@ -1121,37 +1095,39 @@ avoid recursion. Thus, the above regular expression can avoid recursion by
being recast as ``Begin [a-zA-Z0-9_ ]*?end``. As a further benefit, such
being recast as ``Begin [a-zA-Z0-9_ ]*?end``. As a further benefit, such
regular expressions will run faster than their recursive equivalents.
regular expressions will run faster than their recursive equivalents.
.. _search-vs-match:
search() vs. match()
search() vs. match()
^^^^^^^^^^^^^^^^^^^^
^^^^^^^^^^^^^^^^^^^^
In a nutshell, :func:`match` only attempts to match a pattern at the beginning
.. sectionauthor:: Fred L. Drake, Jr. <fdrake@acm.org>
of a string where :func:`search` will match a pattern anywhere in a string.
For example:
>>> re.match("o", "dog") # No match as "o" is not the first letter of "dog".
Python offers two different primitive operations based on regular expressions:
>>> re.search("o", "dog") # Match as search() looks everywhere in the string.
:func:`re.match` checks for a match only at the beginning of the string, while
<_sre.SRE_Match object at ...>
:func:`re.search` checks for a match anywhere in the string (this is what Perl
does by default).
.. not
e::
For exampl
e::
The following applies only to regular expression objects like those created
>>> re.match("c", "abcdef") # No match
with ``re.compile("pattern")``, not the primitives ``re.match(pattern,
>>> re.search("c", "abcdef") # Match
string)`` or ``re.search(pattern, string)``.
<_sre.SRE_Match object at ...>
:func:`match` has an optional second parameter that gives an index in the string
Regular expressions beginning with ``'^'`` can be used with :func:`search` to
where the search is to start
::
restrict the match at the beginning of the string
::
>>> pattern = re.compile("o")
>>> re.match("c", "abcdef") # No match
>>> pattern.match("dog") # No match as "o" is not at the start of "dog."
>>> re.search("^c", "abcdef") # No match
>>> re.search("^a", "abcdef") # Match
<_sre.SRE_Match object at ...>
# Equivalent to the above expression as 0 is the default starting index:
Note however that in :const:`MULTILINE` mode :func:`match` only matches at the
>>> pattern.match("dog", 0)
beginning of the string, whereas using :func:`search` with a regular expression
beginning with ``'^'`` will match at the beginning of each line.
# Match as "o" is the 2nd character of "dog" (index 0 is the first):
>>> re.match('X', 'A\nB\nX', re.MULTILINE) # No match
>>>
pattern.match("dog", 1)
>>>
re.search('^X', 'A\nB\nX', re.MULTILINE) # Match
<_sre.SRE_Match object at ...>
<_sre.SRE_Match object at ...>
>>> pattern.match("dog", 2) # No match as "o" is not the 3rd character of "dog."
Making a Phonebook
Making a Phonebook
...
...
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