Skip to content

Commit 7571941

Browse files
committed
python#19639: update the repr of the match objects in the docs. Patch by Claudiu Popa.
1 parent 7cd9fbe commit 7571941

File tree

3 files changed

+18
-18
lines changed

3 files changed

+18
-18
lines changed

Doc/howto/regex.rst

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -402,7 +402,7 @@ should store the result in a variable for later use. ::
402402

403403
>>> m = p.match('tempo')
404404
>>> m #doctest: +ELLIPSIS
405-
<_sre.SRE_Match object at 0x...>
405+
<_sre.SRE_Match object; span=(0, 5), match='tempo'>
406406

407407
Now you can query the :ref:`match object <match-objects>` for information
408408
about the matching string. :ref:`match object <match-objects>` instances
@@ -441,7 +441,7 @@ case. ::
441441
>>> print(p.match('::: message'))
442442
None
443443
>>> m = p.search('::: message'); print(m) #doctest: +ELLIPSIS
444-
<_sre.SRE_Match object at 0x...>
444+
<_sre.SRE_Match object; span=(4, 11), match='message'>
445445
>>> m.group()
446446
'message'
447447
>>> m.span()
@@ -493,7 +493,7 @@ the RE string added as the first argument, and still return either ``None`` or a
493493
>>> print(re.match(r'From\s+', 'Fromage amk'))
494494
None
495495
>>> re.match(r'From\s+', 'From amk Thu May 14 19:12:10 1998') #doctest: +ELLIPSIS
496-
<_sre.SRE_Match object at 0x...>
496+
<_sre.SRE_Match object; span=(0, 5), match='From '>
497497

498498
Under the hood, these functions simply create a pattern object for you
499499
and call the appropriate method on it. They also store the compiled
@@ -685,7 +685,7 @@ given location, they can obviously be matched an infinite number of times.
685685
line, the RE to use is ``^From``. ::
686686

687687
>>> print(re.search('^From', 'From Here to Eternity')) #doctest: +ELLIPSIS
688-
<_sre.SRE_Match object at 0x...>
688+
<_sre.SRE_Match object; span=(0, 4), match='From'>
689689
>>> print(re.search('^From', 'Reciting From Memory'))
690690
None
691691

@@ -697,11 +697,11 @@ given location, they can obviously be matched an infinite number of times.
697697
or any location followed by a newline character. ::
698698

699699
>>> print(re.search('}$', '{block}')) #doctest: +ELLIPSIS
700-
<_sre.SRE_Match object at 0x...>
700+
<_sre.SRE_Match object; span=(6, 7), match='}'>
701701
>>> print(re.search('}$', '{block} '))
702702
None
703703
>>> print(re.search('}$', '{block}\n')) #doctest: +ELLIPSIS
704-
<_sre.SRE_Match object at 0x...>
704+
<_sre.SRE_Match object; span=(6, 7), match='}'>
705705

706706
To match a literal ``'$'``, use ``\$`` or enclose it inside a character class,
707707
as in ``[$]``.
@@ -726,7 +726,7 @@ given location, they can obviously be matched an infinite number of times.
726726

727727
>>> p = re.compile(r'\bclass\b')
728728
>>> print(p.search('no class at all')) #doctest: +ELLIPSIS
729-
<_sre.SRE_Match object at 0x...>
729+
<_sre.SRE_Match object; span=(3, 8), match='class'>
730730
>>> print(p.search('the declassified algorithm'))
731731
None
732732
>>> print(p.search('one subclass is'))
@@ -744,7 +744,7 @@ given location, they can obviously be matched an infinite number of times.
744744
>>> print(p.search('no class at all'))
745745
None
746746
>>> print(p.search('\b' + 'class' + '\b')) #doctest: +ELLIPSIS
747-
<_sre.SRE_Match object at 0x...>
747+
<_sre.SRE_Match object; span=(0, 7), match='\x08class\x08'>
748748

749749
Second, inside a character class, where there's no use for this assertion,
750750
``\b`` represents the backspace character, for compatibility with Python's

Doc/library/fnmatch.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ patterns.
8686
'.*\\.txt$'
8787
>>> reobj = re.compile(regex)
8888
>>> reobj.match('foobar.txt')
89-
<_sre.SRE_Match object at 0x...>
89+
<_sre.SRE_Match object; span=(0, 10), match='foobar.txt'>
9090

9191

9292
.. seealso::

Doc/library/re.rst

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -755,7 +755,7 @@ attributes:
755755

756756
>>> pattern = re.compile("d")
757757
>>> pattern.search("dog") # Match at index 0
758-
<_sre.SRE_Match object at ...>
758+
<_sre.SRE_Match object; span=(0, 1), match='d'>
759759
>>> pattern.search("dog", 1) # No match; search doesn't include the "d"
760760

761761

@@ -772,7 +772,7 @@ attributes:
772772
>>> pattern = re.compile("o")
773773
>>> pattern.match("dog") # No match as "o" is not at the start of "dog".
774774
>>> pattern.match("dog", 1) # Match as "o" is the 2nd character of "dog".
775-
<_sre.SRE_Match object at ...>
775+
<_sre.SRE_Match object; span=(1, 2), match='o'>
776776

777777
If you want to locate a match anywhere in *string*, use
778778
:meth:`~regex.search` instead (see also :ref:`search-vs-match`).
@@ -1139,23 +1139,23 @@ For example::
11391139

11401140
>>> re.match("c", "abcdef") # No match
11411141
>>> re.search("c", "abcdef") # Match
1142-
<_sre.SRE_Match object at ...>
1142+
<_sre.SRE_Match object; span=(2, 3), match='c'>
11431143

11441144
Regular expressions beginning with ``'^'`` can be used with :func:`search` to
11451145
restrict the match at the beginning of the string::
11461146

11471147
>>> re.match("c", "abcdef") # No match
11481148
>>> re.search("^c", "abcdef") # No match
11491149
>>> re.search("^a", "abcdef") # Match
1150-
<_sre.SRE_Match object at ...>
1150+
<_sre.SRE_Match object; span=(0, 1), match='a'>
11511151

11521152
Note however that in :const:`MULTILINE` mode :func:`match` only matches at the
11531153
beginning of the string, whereas using :func:`search` with a regular expression
11541154
beginning with ``'^'`` will match at the beginning of each line.
11551155

11561156
>>> re.match('X', 'A\nB\nX', re.MULTILINE) # No match
11571157
>>> re.search('^X', 'A\nB\nX', re.MULTILINE) # Match
1158-
<_sre.SRE_Match object at ...>
1158+
<_sre.SRE_Match object; span=(4, 5), match='X'>
11591159

11601160

11611161
Making a Phonebook
@@ -1274,19 +1274,19 @@ another one to escape it. For example, the two following lines of code are
12741274
functionally identical:
12751275

12761276
>>> re.match(r"\W(.)\1\W", " ff ")
1277-
<_sre.SRE_Match object at ...>
1277+
<_sre.SRE_Match object; span=(0, 4), match=' ff '>
12781278
>>> re.match("\\W(.)\\1\\W", " ff ")
1279-
<_sre.SRE_Match object at ...>
1279+
<_sre.SRE_Match object; span=(0, 4), match=' ff '>
12801280

12811281
When one wants to match a literal backslash, it must be escaped in the regular
12821282
expression. With raw string notation, this means ``r"\\"``. Without raw string
12831283
notation, one must use ``"\\\\"``, making the following lines of code
12841284
functionally identical:
12851285

12861286
>>> re.match(r"\\", r"\\")
1287-
<_sre.SRE_Match object at ...>
1287+
<_sre.SRE_Match object; span=(0, 1), match='\\'>
12881288
>>> re.match("\\\\", r"\\")
1289-
<_sre.SRE_Match object at ...>
1289+
<_sre.SRE_Match object; span=(0, 1), match='\\'>
12901290

12911291

12921292
Writing a Tokenizer

0 commit comments

Comments
 (0)