Skip to content

Commit d6090bb

Browse files
committed
Replace name-parsing examples with Monty Python references
Using first_name/last_name patterns promotes the myth that names have simple, universal structures. Replace with: - "killer rabbit" with adjective/animal groups - "Norwegian Blue, pining for the fjords" for unlabeled groups
1 parent 3cd9d71 commit d6090bb

File tree

1 file changed

+23
-23
lines changed

1 file changed

+23
-23
lines changed

Doc/library/re.rst

Lines changed: 23 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1446,15 +1446,15 @@ when there is no match, you can test whether there was a match with a simple
14461446
If a group is contained in a part of the pattern that matched multiple times,
14471447
the last match is returned. ::
14481448

1449-
>>> m = re.search(r"\A(\w+) (\w+)", "Isaac Newton, physicist")
1449+
>>> m = re.search(r"\A(\w+) (\w+)", "Norwegian Blue, pining for the fjords")
14501450
>>> m.group(0) # The entire match
1451-
'Isaac Newton'
1451+
'Norwegian Blue'
14521452
>>> m.group(1) # The first parenthesized subgroup.
1453-
'Isaac'
1453+
'Norwegian'
14541454
>>> m.group(2) # The second parenthesized subgroup.
1455-
'Newton'
1455+
'Blue'
14561456
>>> m.group(1, 2) # Multiple arguments give us a tuple.
1457-
('Isaac', 'Newton')
1457+
('Norwegian', 'Blue')
14581458

14591459
If the regular expression uses the ``(?P<name>...)`` syntax, the *groupN*
14601460
arguments may also be strings identifying groups by their group name. If a
@@ -1463,18 +1463,18 @@ when there is no match, you can test whether there was a match with a simple
14631463

14641464
A moderately complicated example::
14651465

1466-
>>> m = re.search(r"(?P<first_name>\w+) (?P<last_name>\w+)", "Malcolm Reynolds")
1467-
>>> m.group('first_name')
1468-
'Malcolm'
1469-
>>> m.group('last_name')
1470-
'Reynolds'
1466+
>>> m = re.search(r"(?P<adjective>\w+) (?P<animal>\w+)", "killer rabbit")
1467+
>>> m.group('adjective')
1468+
'killer'
1469+
>>> m.group('animal')
1470+
'rabbit'
14711471

14721472
Named groups can also be referred to by their index::
14731473

14741474
>>> m.group(1)
1475-
'Malcolm'
1475+
'killer'
14761476
>>> m.group(2)
1477-
'Reynolds'
1477+
'rabbit'
14781478

14791479
If a group matches multiple times, only the last match is accessible::
14801480

@@ -1488,21 +1488,21 @@ when there is no match, you can test whether there was a match with a simple
14881488
This is identical to ``m.group(g)``. This allows easier access to
14891489
an individual group from a match::
14901490

1491-
>>> m = re.search(r"(\w+) (\w+)", "Isaac Newton, physicist")
1491+
>>> m = re.search(r"(\w+) (\w+)", "Norwegian Blue, pining for the fjords")
14921492
>>> m[0] # The entire match
1493-
'Isaac Newton'
1493+
'Norwegian Blue'
14941494
>>> m[1] # The first parenthesized subgroup.
1495-
'Isaac'
1495+
'Norwegian'
14961496
>>> m[2] # The second parenthesized subgroup.
1497-
'Newton'
1497+
'Blue'
14981498

14991499
Named groups are supported as well::
15001500

1501-
>>> m = re.search(r"(?P<first_name>\w+) (?P<last_name>\w+)", "Isaac Newton")
1502-
>>> m['first_name']
1503-
'Isaac'
1504-
>>> m['last_name']
1505-
'Newton'
1501+
>>> m = re.search(r"(?P<adjective>\w+) (?P<animal>\w+)", "killer rabbit")
1502+
>>> m['adjective']
1503+
'killer'
1504+
>>> m['animal']
1505+
'rabbit'
15061506

15071507
.. versionadded:: 3.6
15081508

@@ -1536,9 +1536,9 @@ when there is no match, you can test whether there was a match with a simple
15361536
the subgroup name. The *default* argument is used for groups that did not
15371537
participate in the match; it defaults to ``None``. For example::
15381538

1539-
>>> m = re.search(r"(?P<first_name>\w+) (?P<last_name>\w+)", "Malcolm Reynolds")
1539+
>>> m = re.search(r"(?P<adjective>\w+) (?P<animal>\w+)", "killer rabbit")
15401540
>>> m.groupdict()
1541-
{'first_name': 'Malcolm', 'last_name': 'Reynolds'}
1541+
{'adjective': 'killer', 'animal': 'rabbit'}
15421542

15431543

15441544
.. method:: Match.start([group])

0 commit comments

Comments
 (0)