PageRenderTime 49ms CodeModel.GetById 11ms RepoModel.GetById 1ms app.codeStats 0ms

/library/diff/re.rst.diff

https://bitbucket.org/pydocja/py26
diff | 689 lines | 555 code | 134 blank | 0 comment | 0 complexity | ac77cef5333a2ced1e7ae1e799eb87bd MD5 | raw file
Possible License(s): BSD-3-Clause
  1. --- r262/library/re.rst 2009-04-06 06:48:06.719229000 +0900
  2. +++ r266/library/re.rst 2010-08-02 06:48:47.574872000 +0900
  3. @@ -207,25 +207,25 @@
  4. currently supported extensions.
  5. ``(?iLmsux)``
  6. (One or more letters from the set ``'i'``, ``'L'``, ``'m'``, ``'s'``,
  7. ``'u'``, ``'x'``.) The group matches the empty string; the letters
  8. set the corresponding flags: :const:`re.I` (ignore case),
  9. :const:`re.L` (locale dependent), :const:`re.M` (multi-line),
  10. :const:`re.S` (dot matches all), :const:`re.U` (Unicode dependent),
  11. and :const:`re.X` (verbose), for the entire regular expression. (The
  12. flags are described in :ref:`contents-of-module-re`.) This
  13. is useful if you wish to include the flags as part of the regular
  14. expression, instead of passing a *flag* argument to the
  15. - :func:`compile` function.
  16. + :func:`re.compile` function.
  17. Note that the ``(?x)`` flag changes how the expression is parsed. It should be
  18. used first in the expression string, or after one or more whitespace characters.
  19. If there are non-whitespace characters before the flag, the results are
  20. undefined.
  21. ``(?:...)``
  22. A non-grouping version of regular parentheses. Matches whatever regular
  23. expression is inside the parentheses, but the substring matched by the group
  24. *cannot* be retrieved after performing a match or referenced later in the
  25. pattern.
  26. @@ -310,25 +310,25 @@
  27. *number* is 0, or *number* is 3 octal digits long, it will not be interpreted as
  28. a group match, but as the character with octal value *number*. Inside the
  29. ``'['`` and ``']'`` of a character class, all numeric escapes are treated as
  30. characters.
  31. ``\A``
  32. Matches only at the start of the string.
  33. ``\b``
  34. Matches the empty string, but only at the beginning or end of a word. A word is
  35. defined as a sequence of alphanumeric or underscore characters, so the end of a
  36. word is indicated by whitespace or a non-alphanumeric, non-underscore character.
  37. - Note that ``\b`` is defined as the boundary between ``\w`` and ``\ W``, so the
  38. + Note that ``\b`` is defined as the boundary between ``\w`` and ``\W``, so the
  39. precise set of characters deemed to be alphanumeric depends on the values of the
  40. ``UNICODE`` and ``LOCALE`` flags. Inside a character range, ``\b`` represents
  41. the backspace character, for compatibility with Python's string literals.
  42. ``\B``
  43. Matches the empty string, but only when it is *not* at the beginning or end of a
  44. word. This is just the opposite of ``\b``, so is also subject to the settings
  45. of ``LOCALE`` and ``UNICODE``.
  46. ``\d``
  47. When the :const:`UNICODE` flag is not specified, matches any decimal digit; this
  48. is equivalent to the set ``[0-9]``. With :const:`UNICODE`, it will match
  49. @@ -434,27 +434,27 @@
  50. Values can be any of the following variables, combined using bitwise OR (the
  51. ``|`` operator).
  52. The sequence ::
  53. prog = re.compile(pattern)
  54. result = prog.match(string)
  55. is equivalent to ::
  56. result = re.match(pattern, string)
  57. - but using :func:`compile` and saving the resulting regular expression object
  58. - for reuse is more efficient when the expression will be used several times
  59. - in a single program.
  60. + but using :func:`re.compile` and saving the resulting regular expression
  61. + object for reuse is more efficient when the expression will be used several
  62. + times in a single program.
  63. .. note::
  64. The compiled versions of the most recent patterns passed to
  65. :func:`re.match`, :func:`re.search` or :func:`re.compile` are cached, so
  66. programs that use only a few regular expressions at a time needn't worry
  67. about compiling regular expressions.
  68. .. data:: I
  69. IGNORECASE
  70. @@ -523,25 +523,25 @@
  71. string.
  72. .. function:: match(pattern, string[, flags])
  73. If zero or more characters at the beginning of *string* match the regular
  74. expression *pattern*, return a corresponding :class:`MatchObject` instance.
  75. Return ``None`` if the string does not match the pattern; note that this is
  76. different from a zero-length match.
  77. .. note::
  78. - If you want to locate a match anywhere in *string*, use :meth:`search`
  79. + If you want to locate a match anywhere in *string*, use :func:`search`
  80. instead.
  81. .. function:: split(pattern, string[, maxsplit=0])
  82. Split *string* by the occurrences of *pattern*. If capturing parentheses are
  83. used in *pattern*, then the text of all groups in the pattern are also returned
  84. as part of the resulting list. If *maxsplit* is nonzero, at most *maxsplit*
  85. splits occur, and the remainder of the string is returned as the final element
  86. of the list. (Incompatibility note: in the original Python 1.5 release,
  87. *maxsplit* was ignored. This has been fixed in later releases.)
  88. @@ -651,307 +651,325 @@
  89. Perform the same operation as :func:`sub`, but return a tuple ``(new_string,
  90. number_of_subs_made)``.
  91. .. function:: escape(string)
  92. Return *string* with all non-alphanumerics backslashed; this is useful if you
  93. want to match an arbitrary literal string that may have regular expression
  94. metacharacters in it.
  95. +.. function:: purge()
  96. +
  97. + Clear the regular expression cache.
  98. +
  99. +
  100. .. exception:: error
  101. Exception raised when a string passed to one of the functions here is not a
  102. valid regular expression (for example, it might contain unmatched parentheses)
  103. or when some other error occurs during compilation or matching. It is never an
  104. error if a string contains no match for a pattern.
  105. .. _re-objects:
  106. Regular Expression Objects
  107. --------------------------
  108. -Compiled regular expression objects support the following methods and
  109. -attributes:
  110. +.. class:: RegexObject
  111. + The :class:`RegexObject` class supports the following methods and attributes:
  112. -.. method:: RegexObject.match(string[, pos[, endpos]])
  113. + .. method:: RegexObject.search(string[, pos[, endpos]])
  114. - If zero or more characters at the beginning of *string* match this regular
  115. - expression, return a corresponding :class:`MatchObject` instance. Return
  116. - ``None`` if the string does not match the pattern; note that this is different
  117. - from a zero-length match.
  118. + Scan through *string* looking for a location where this regular expression
  119. + produces a match, and return a corresponding :class:`MatchObject` instance.
  120. + Return ``None`` if no position in the string matches the pattern; note that this
  121. + is different from finding a zero-length match at some point in the string.
  122. +
  123. + The optional second parameter *pos* gives an index in the string where the
  124. + search is to start; it defaults to ``0``. This is not completely equivalent to
  125. + slicing the string; the ``'^'`` pattern character matches at the real beginning
  126. + of the string and at positions just after a newline, but not necessarily at the
  127. + index where the search is to start.
  128. +
  129. + The optional parameter *endpos* limits how far the string will be searched; it
  130. + will be as if the string is *endpos* characters long, so only the characters
  131. + from *pos* to ``endpos - 1`` will be searched for a match. If *endpos* is less
  132. + than *pos*, no match will be found, otherwise, if *rx* is a compiled regular
  133. + expression object, ``rx.search(string, 0, 50)`` is equivalent to
  134. + ``rx.search(string[:50], 0)``.
  135. - .. note::
  136. + >>> pattern = re.compile("d")
  137. + >>> pattern.search("dog") # Match at index 0
  138. + <_sre.SRE_Match object at ...>
  139. + >>> pattern.search("dog", 1) # No match; search doesn't include the "d"
  140. - If you want to locate a match anywhere in *string*, use :meth:`search`
  141. - instead.
  142. - The optional second parameter *pos* gives an index in the string where the
  143. - search is to start; it defaults to ``0``. This is not completely equivalent to
  144. - slicing the string; the ``'^'`` pattern character matches at the real beginning
  145. - of the string and at positions just after a newline, but not necessarily at the
  146. - index where the search is to start.
  147. -
  148. - The optional parameter *endpos* limits how far the string will be searched; it
  149. - will be as if the string is *endpos* characters long, so only the characters
  150. - from *pos* to ``endpos - 1`` will be searched for a match. If *endpos* is less
  151. - than *pos*, no match will be found, otherwise, if *rx* is a compiled regular
  152. - expression object, ``rx.match(string, 0, 50)`` is equivalent to
  153. - ``rx.match(string[:50], 0)``.
  154. + .. method:: RegexObject.match(string[, pos[, endpos]])
  155. - >>> pattern = re.compile("o")
  156. - >>> pattern.match("dog") # No match as "o" is not at the start of "dog."
  157. - >>> pattern.match("dog", 1) # Match as "o" is the 2nd character of "dog".
  158. - <_sre.SRE_Match object at ...>
  159. + If zero or more characters at the *beginning* of *string* match this regular
  160. + expression, return a corresponding :class:`MatchObject` instance. Return
  161. + ``None`` if the string does not match the pattern; note that this is different
  162. + from a zero-length match.
  163. + The optional *pos* and *endpos* parameters have the same meaning as for the
  164. + :meth:`~RegexObject.search` method.
  165. -.. method:: RegexObject.search(string[, pos[, endpos]])
  166. + .. note::
  167. - Scan through *string* looking for a location where this regular expression
  168. - produces a match, and return a corresponding :class:`MatchObject` instance.
  169. - Return ``None`` if no position in the string matches the pattern; note that this
  170. - is different from finding a zero-length match at some point in the string.
  171. + If you want to locate a match anywhere in *string*, use
  172. + :meth:`~RegexObject.search` instead.
  173. - The optional *pos* and *endpos* parameters have the same meaning as for the
  174. - :meth:`match` method.
  175. + >>> pattern = re.compile("o")
  176. + >>> pattern.match("dog") # No match as "o" is not at the start of "dog".
  177. + >>> pattern.match("dog", 1) # Match as "o" is the 2nd character of "dog".
  178. + <_sre.SRE_Match object at ...>
  179. -.. method:: RegexObject.split(string[, maxsplit=0])
  180. + .. method:: RegexObject.split(string[, maxsplit=0])
  181. - Identical to the :func:`split` function, using the compiled pattern.
  182. + Identical to the :func:`split` function, using the compiled pattern.
  183. -.. method:: RegexObject.findall(string[, pos[, endpos]])
  184. + .. method:: RegexObject.findall(string[, pos[, endpos]])
  185. - Identical to the :func:`findall` function, using the compiled pattern.
  186. + Similar to the :func:`findall` function, using the compiled pattern, but
  187. + also accepts optional *pos* and *endpos* parameters that limit the search
  188. + region like for :meth:`match`.
  189. -.. method:: RegexObject.finditer(string[, pos[, endpos]])
  190. + .. method:: RegexObject.finditer(string[, pos[, endpos]])
  191. - Identical to the :func:`finditer` function, using the compiled pattern.
  192. + Similar to the :func:`finditer` function, using the compiled pattern, but
  193. + also accepts optional *pos* and *endpos* parameters that limit the search
  194. + region like for :meth:`match`.
  195. -.. method:: RegexObject.sub(repl, string[, count=0])
  196. + .. method:: RegexObject.sub(repl, string[, count=0])
  197. - Identical to the :func:`sub` function, using the compiled pattern.
  198. + Identical to the :func:`sub` function, using the compiled pattern.
  199. -.. method:: RegexObject.subn(repl, string[, count=0])
  200. + .. method:: RegexObject.subn(repl, string[, count=0])
  201. - Identical to the :func:`subn` function, using the compiled pattern.
  202. + Identical to the :func:`subn` function, using the compiled pattern.
  203. -.. attribute:: RegexObject.flags
  204. + .. attribute:: RegexObject.flags
  205. - The flags argument used when the RE object was compiled, or ``0`` if no flags
  206. - were provided.
  207. + The flags argument used when the RE object was compiled, or ``0`` if no flags
  208. + were provided.
  209. -.. attribute:: RegexObject.groups
  210. + .. attribute:: RegexObject.groups
  211. - The number of capturing groups in the pattern.
  212. + The number of capturing groups in the pattern.
  213. -.. attribute:: RegexObject.groupindex
  214. + .. attribute:: RegexObject.groupindex
  215. - A dictionary mapping any symbolic group names defined by ``(?P<id>)`` to group
  216. - numbers. The dictionary is empty if no symbolic groups were used in the
  217. - pattern.
  218. + A dictionary mapping any symbolic group names defined by ``(?P<id>)`` to group
  219. + numbers. The dictionary is empty if no symbolic groups were used in the
  220. + pattern.
  221. -.. attribute:: RegexObject.pattern
  222. + .. attribute:: RegexObject.pattern
  223. - The pattern string from which the RE object was compiled.
  224. + The pattern string from which the RE object was compiled.
  225. .. _match-objects:
  226. Match Objects
  227. -------------
  228. -Match objects always have a boolean value of :const:`True`, so that you can test
  229. -whether e.g. :func:`match` resulted in a match with a simple if statement. They
  230. -support the following methods and attributes:
  231. -
  232. -
  233. -.. method:: MatchObject.expand(template)
  234. -
  235. - Return the string obtained by doing backslash substitution on the template
  236. - string *template*, as done by the :meth:`sub` method. Escapes such as ``\n`` are
  237. - converted to the appropriate characters, and numeric backreferences (``\1``,
  238. - ``\2``) and named backreferences (``\g<1>``, ``\g<name>``) are replaced by the
  239. - contents of the corresponding group.
  240. -
  241. -
  242. -.. method:: MatchObject.group([group1, ...])
  243. -
  244. - Returns one or more subgroups of the match. If there is a single argument, the
  245. - result is a single string; if there are multiple arguments, the result is a
  246. - tuple with one item per argument. Without arguments, *group1* defaults to zero
  247. - (the whole match is returned). If a *groupN* argument is zero, the corresponding
  248. - return value is the entire matching string; if it is in the inclusive range
  249. - [1..99], it is the string matching the corresponding parenthesized group. If a
  250. - group number is negative or larger than the number of groups defined in the
  251. - pattern, an :exc:`IndexError` exception is raised. If a group is contained in a
  252. - part of the pattern that did not match, the corresponding result is ``None``.
  253. - If a group is contained in a part of the pattern that matched multiple times,
  254. - the last match is returned.
  255. -
  256. - >>> m = re.match(r"(\w+) (\w+)", "Isaac Newton, physicist")
  257. - >>> m.group(0) # The entire match
  258. - 'Isaac Newton'
  259. - >>> m.group(1) # The first parenthesized subgroup.
  260. - 'Isaac'
  261. - >>> m.group(2) # The second parenthesized subgroup.
  262. - 'Newton'
  263. - >>> m.group(1, 2) # Multiple arguments give us a tuple.
  264. - ('Isaac', 'Newton')
  265. -
  266. - If the regular expression uses the ``(?P<name>...)`` syntax, the *groupN*
  267. - arguments may also be strings identifying groups by their group name. If a
  268. - string argument is not used as a group name in the pattern, an :exc:`IndexError`
  269. - exception is raised.
  270. -
  271. - A moderately complicated example:
  272. -
  273. - >>> m = re.match(r"(?P<first_name>\w+) (?P<last_name>\w+)", "Malcom Reynolds")
  274. - >>> m.group('first_name')
  275. - 'Malcom'
  276. - >>> m.group('last_name')
  277. - 'Reynolds'
  278. -
  279. - Named groups can also be referred to by their index:
  280. -
  281. - >>> m.group(1)
  282. - 'Malcom'
  283. - >>> m.group(2)
  284. - 'Reynolds'
  285. -
  286. - If a group matches multiple times, only the last match is accessible:
  287. -
  288. - >>> m = re.match(r"(..)+", "a1b2c3") # Matches 3 times.
  289. - >>> m.group(1) # Returns only the last match.
  290. - 'c3'
  291. -
  292. -
  293. -.. method:: MatchObject.groups([default])
  294. -
  295. - Return a tuple containing all the subgroups of the match, from 1 up to however
  296. - many groups are in the pattern. The *default* argument is used for groups that
  297. - did not participate in the match; it defaults to ``None``. (Incompatibility
  298. - note: in the original Python 1.5 release, if the tuple was one element long, a
  299. - string would be returned instead. In later versions (from 1.5.1 on), a
  300. - singleton tuple is returned in such cases.)
  301. +.. class:: MatchObject
  302. - For example:
  303. + Match Objects always have a boolean value of :const:`True`, so that you can test
  304. + whether e.g. :func:`match` resulted in a match with a simple if statement. They
  305. + support the following methods and attributes:
  306. - >>> m = re.match(r"(\d+)\.(\d+)", "24.1632")
  307. - >>> m.groups()
  308. - ('24', '1632')
  309. - If we make the decimal place and everything after it optional, not all groups
  310. - might participate in the match. These groups will default to ``None`` unless
  311. - the *default* argument is given:
  312. + .. method:: MatchObject.expand(template)
  313. - >>> m = re.match(r"(\d+)\.?(\d+)?", "24")
  314. - >>> m.groups() # Second group defaults to None.
  315. - ('24', None)
  316. - >>> m.groups('0') # Now, the second group defaults to '0'.
  317. - ('24', '0')
  318. + Return the string obtained by doing backslash substitution on the template
  319. + string *template*, as done by the :meth:`~RegexObject.sub` method. Escapes
  320. + such as ``\n`` are converted to the appropriate characters, and numeric
  321. + backreferences (``\1``, ``\2``) and named backreferences (``\g<1>``,
  322. + ``\g<name>``) are replaced by the contents of the corresponding group.
  323. -.. method:: MatchObject.groupdict([default])
  324. + .. method:: MatchObject.group([group1, ...])
  325. - Return a dictionary containing all the *named* subgroups of the match, keyed by
  326. - the subgroup name. The *default* argument is used for groups that did not
  327. - participate in the match; it defaults to ``None``. For example:
  328. + Returns one or more subgroups of the match. If there is a single argument, the
  329. + result is a single string; if there are multiple arguments, the result is a
  330. + tuple with one item per argument. Without arguments, *group1* defaults to zero
  331. + (the whole match is returned). If a *groupN* argument is zero, the corresponding
  332. + return value is the entire matching string; if it is in the inclusive range
  333. + [1..99], it is the string matching the corresponding parenthesized group. If a
  334. + group number is negative or larger than the number of groups defined in the
  335. + pattern, an :exc:`IndexError` exception is raised. If a group is contained in a
  336. + part of the pattern that did not match, the corresponding result is ``None``.
  337. + If a group is contained in a part of the pattern that matched multiple times,
  338. + the last match is returned.
  339. - >>> m = re.match(r"(?P<first_name>\w+) (?P<last_name>\w+)", "Malcom Reynolds")
  340. - >>> m.groupdict()
  341. - {'first_name': 'Malcom', 'last_name': 'Reynolds'}
  342. + >>> m = re.match(r"(\w+) (\w+)", "Isaac Newton, physicist")
  343. + >>> m.group(0) # The entire match
  344. + 'Isaac Newton'
  345. + >>> m.group(1) # The first parenthesized subgroup.
  346. + 'Isaac'
  347. + >>> m.group(2) # The second parenthesized subgroup.
  348. + 'Newton'
  349. + >>> m.group(1, 2) # Multiple arguments give us a tuple.
  350. + ('Isaac', 'Newton')
  351. + If the regular expression uses the ``(?P<name>...)`` syntax, the *groupN*
  352. + arguments may also be strings identifying groups by their group name. If a
  353. + string argument is not used as a group name in the pattern, an :exc:`IndexError`
  354. + exception is raised.
  355. -.. method:: MatchObject.start([group])
  356. - MatchObject.end([group])
  357. + A moderately complicated example:
  358. - Return the indices of the start and end of the substring matched by *group*;
  359. - *group* defaults to zero (meaning the whole matched substring). Return ``-1`` if
  360. - *group* exists but did not contribute to the match. For a match object *m*, and
  361. - a group *g* that did contribute to the match, the substring matched by group *g*
  362. - (equivalent to ``m.group(g)``) is ::
  363. + >>> m = re.match(r"(?P<first_name>\w+) (?P<last_name>\w+)", "Malcolm Reynolds")
  364. + >>> m.group('first_name')
  365. + 'Malcolm'
  366. + >>> m.group('last_name')
  367. + 'Reynolds'
  368. - m.string[m.start(g):m.end(g)]
  369. + Named groups can also be referred to by their index:
  370. - Note that ``m.start(group)`` will equal ``m.end(group)`` if *group* matched a
  371. - null string. For example, after ``m = re.search('b(c?)', 'cba')``,
  372. - ``m.start(0)`` is 1, ``m.end(0)`` is 2, ``m.start(1)`` and ``m.end(1)`` are both
  373. - 2, and ``m.start(2)`` raises an :exc:`IndexError` exception.
  374. + >>> m.group(1)
  375. + 'Malcolm'
  376. + >>> m.group(2)
  377. + 'Reynolds'
  378. - An example that will remove *remove_this* from email addresses:
  379. + If a group matches multiple times, only the last match is accessible:
  380. - >>> email = "tony@tiremove_thisger.net"
  381. - >>> m = re.search("remove_this", email)
  382. - >>> email[:m.start()] + email[m.end():]
  383. - 'tony@tiger.net'
  384. + >>> m = re.match(r"(..)+", "a1b2c3") # Matches 3 times.
  385. + >>> m.group(1) # Returns only the last match.
  386. + 'c3'
  387. -.. method:: MatchObject.span([group])
  388. + .. method:: MatchObject.groups([default])
  389. - For :class:`MatchObject` *m*, return the 2-tuple ``(m.start(group),
  390. - m.end(group))``. Note that if *group* did not contribute to the match, this is
  391. - ``(-1, -1)``. *group* defaults to zero, the entire match.
  392. + Return a tuple containing all the subgroups of the match, from 1 up to however
  393. + many groups are in the pattern. The *default* argument is used for groups that
  394. + did not participate in the match; it defaults to ``None``. (Incompatibility
  395. + note: in the original Python 1.5 release, if the tuple was one element long, a
  396. + string would be returned instead. In later versions (from 1.5.1 on), a
  397. + singleton tuple is returned in such cases.)
  398. + For example:
  399. -.. attribute:: MatchObject.pos
  400. + >>> m = re.match(r"(\d+)\.(\d+)", "24.1632")
  401. + >>> m.groups()
  402. + ('24', '1632')
  403. - The value of *pos* which was passed to the :func:`search` or :func:`match`
  404. - method of the :class:`RegexObject`. This is the index into the string at which
  405. - the RE engine started looking for a match.
  406. + If we make the decimal place and everything after it optional, not all groups
  407. + might participate in the match. These groups will default to ``None`` unless
  408. + the *default* argument is given:
  409. + >>> m = re.match(r"(\d+)\.?(\d+)?", "24")
  410. + >>> m.groups() # Second group defaults to None.
  411. + ('24', None)
  412. + >>> m.groups('0') # Now, the second group defaults to '0'.
  413. + ('24', '0')
  414. -.. attribute:: MatchObject.endpos
  415. - The value of *endpos* which was passed to the :func:`search` or :func:`match`
  416. - method of the :class:`RegexObject`. This is the index into the string beyond
  417. - which the RE engine will not go.
  418. + .. method:: MatchObject.groupdict([default])
  419. + Return a dictionary containing all the *named* subgroups of the match, keyed by
  420. + the subgroup name. The *default* argument is used for groups that did not
  421. + participate in the match; it defaults to ``None``. For example:
  422. -.. attribute:: MatchObject.lastindex
  423. + >>> m = re.match(r"(?P<first_name>\w+) (?P<last_name>\w+)", "Malcolm Reynolds")
  424. + >>> m.groupdict()
  425. + {'first_name': 'Malcolm', 'last_name': 'Reynolds'}
  426. - The integer index of the last matched capturing group, or ``None`` if no group
  427. - was matched at all. For example, the expressions ``(a)b``, ``((a)(b))``, and
  428. - ``((ab))`` will have ``lastindex == 1`` if applied to the string ``'ab'``, while
  429. - the expression ``(a)(b)`` will have ``lastindex == 2``, if applied to the same
  430. - string.
  431. +
  432. + .. method:: MatchObject.start([group])
  433. + MatchObject.end([group])
  434. +
  435. + Return the indices of the start and end of the substring matched by *group*;
  436. + *group* defaults to zero (meaning the whole matched substring). Return ``-1`` if
  437. + *group* exists but did not contribute to the match. For a match object *m*, and
  438. + a group *g* that did contribute to the match, the substring matched by group *g*
  439. + (equivalent to ``m.group(g)``) is ::
  440. +
  441. + m.string[m.start(g):m.end(g)]
  442. +
  443. + Note that ``m.start(group)`` will equal ``m.end(group)`` if *group* matched a
  444. + null string. For example, after ``m = re.search('b(c?)', 'cba')``,
  445. + ``m.start(0)`` is 1, ``m.end(0)`` is 2, ``m.start(1)`` and ``m.end(1)`` are both
  446. + 2, and ``m.start(2)`` raises an :exc:`IndexError` exception.
  447. +
  448. + An example that will remove *remove_this* from email addresses:
  449. +
  450. + >>> email = "tony@tiremove_thisger.net"
  451. + >>> m = re.search("remove_this", email)
  452. + >>> email[:m.start()] + email[m.end():]
  453. + 'tony@tiger.net'
  454. +
  455. +
  456. + .. method:: MatchObject.span([group])
  457. +
  458. + For :class:`MatchObject` *m*, return the 2-tuple ``(m.start(group),
  459. + m.end(group))``. Note that if *group* did not contribute to the match, this is
  460. + ``(-1, -1)``. *group* defaults to zero, the entire match.
  461. +
  462. +
  463. + .. attribute:: MatchObject.pos
  464. +
  465. + The value of *pos* which was passed to the :meth:`~RegexObject.search` or
  466. + :meth:`~RegexObject.match` method of the :class:`RegexObject`. This is the
  467. + index into the string at which the RE engine started looking for a match.
  468. +
  469. +
  470. + .. attribute:: MatchObject.endpos
  471. +
  472. + The value of *endpos* which was passed to the :meth:`~RegexObject.search` or
  473. + :meth:`~RegexObject.match` method of the :class:`RegexObject`. This is the
  474. + index into the string beyond which the RE engine will not go.
  475. +
  476. +
  477. + .. attribute:: MatchObject.lastindex
  478. +
  479. + The integer index of the last matched capturing group, or ``None`` if no group
  480. + was matched at all. For example, the expressions ``(a)b``, ``((a)(b))``, and
  481. + ``((ab))`` will have ``lastindex == 1`` if applied to the string ``'ab'``, while
  482. + the expression ``(a)(b)`` will have ``lastindex == 2``, if applied to the same
  483. + string.
  484. -.. attribute:: MatchObject.lastgroup
  485. + .. attribute:: MatchObject.lastgroup
  486. - The name of the last matched capturing group, or ``None`` if the group didn't
  487. - have a name, or if no group was matched at all.
  488. + The name of the last matched capturing group, or ``None`` if the group didn't
  489. + have a name, or if no group was matched at all.
  490. -.. attribute:: MatchObject.re
  491. + .. attribute:: MatchObject.re
  492. - The regular expression object whose :meth:`match` or :meth:`search` method
  493. - produced this :class:`MatchObject` instance.
  494. + The regular expression object whose :meth:`~RegexObject.match` or
  495. + :meth:`~RegexObject.search` method produced this :class:`MatchObject`
  496. + instance.
  497. -.. attribute:: MatchObject.string
  498. + .. attribute:: MatchObject.string
  499. - The string passed to :func:`match` or :func:`search`.
  500. + The string passed to :meth:`~RegexObject.match` or
  501. + :meth:`~RegexObject.search`.
  502. Examples
  503. --------
  504. Checking For a Pair
  505. ^^^^^^^^^^^^^^^^^^^
  506. In this example, we'll use the following helper function to display match
  507. objects a little more gracefully:
  508. @@ -978,26 +996,27 @@
  509. "<Match: '727ak', groups=()>"
  510. That last hand, ``"727ak"``, contained a pair, or two of the same valued cards.
  511. To match this with a regular expression, one could use backreferences as such:
  512. >>> pair = re.compile(r".*(.).*\1")
  513. >>> displaymatch(pair.match("717ak")) # Pair of 7s.
  514. "<Match: '717', groups=('7',)>"
  515. >>> displaymatch(pair.match("718ak")) # No pairs.
  516. >>> displaymatch(pair.match("354aa")) # Pair of aces.
  517. "<Match: '354aa', groups=('a',)>"
  518. -To find out what card the pair consists of, one could use the :func:`group`
  519. -method of :class:`MatchObject` in the following manner:
  520. +To find out what card the pair consists of, one could use the
  521. +:meth:`~MatchObject.group` method of :class:`MatchObject` in the following
  522. +manner:
  523. .. doctest::
  524. >>> pair.match("717ak").group(1)
  525. '7'
  526. # Error because re.match() returns None, which doesn't have a group() method:
  527. >>> pair.match("718ak").group(1)
  528. Traceback (most recent call last):
  529. File "<pyshell#23>", line 1, in <module>
  530. re.match(r".*(.).*\1", "718ak").group(1)
  531. AttributeError: 'NoneType' object has no attribute 'group'
  532. @@ -1170,27 +1189,27 @@
  533. ^^^^^^^^^^^^
  534. :func:`sub` replaces every occurrence of a pattern with a string or the
  535. result of a function. This example demonstrates using :func:`sub` with
  536. a function to "munge" text, or randomize the order of all the characters
  537. in each word of a sentence except for the first and last characters::
  538. >>> def repl(m):
  539. ... inner_word = list(m.group(2))
  540. ... random.shuffle(inner_word)
  541. ... return m.group(1) + "".join(inner_word) + m.group(3)
  542. >>> text = "Professor Abdolmalek, please report your absences promptly."
  543. - >>> re.sub("(\w)(\w+)(\w)", repl, text)
  544. + >>> re.sub(r"(\w)(\w+)(\w)", repl, text)
  545. 'Poefsrosr Aealmlobdk, pslaee reorpt your abnseces plmrptoy.'
  546. - >>> re.sub("(\w)(\w+)(\w)", repl, text)
  547. + >>> re.sub(r"(\w)(\w+)(\w)", repl, text)
  548. 'Pofsroser Aodlambelk, plasee reoprt yuor asnebces potlmrpy.'
  549. Finding all Adverbs
  550. ^^^^^^^^^^^^^^^^^^^
  551. :func:`findall` matches *all* occurrences of a pattern, not just the first
  552. one as :func:`search` does. For example, if one was a writer and wanted to
  553. find all of the adverbs in some text, he or she might use :func:`findall` in
  554. the following manner:
  555. >>> text = "He was carefully disguised but captured quickly by police."