Movatterモバイル変換


[0]ホーム

URL:


Previous PageUp One LevelNext PagePython Library ReferenceContentsModule IndexIndex
Previous:4.2.4 Regular Expression ObjectsUp:4.2 reNext:4.2.6 Examples

 
4.2.5 Match Objects

MatchObject instances support the following methods andattributes:

expand(template)
Return the string obtained by doing backslash substitution on thetemplate stringtemplate, as done by thesub() method.Escapes such as "\n" are converted to the appropriatecharacters, and numeric backreferences ("\1", "\2") andnamed backreferences ("\g<1>", "\g<name>") are replacedby the contents of the corresponding group.

group([group1, ...])
Returns one or more subgroups of the match. If there is a singleargument, the result is a single string; if there aremultiple arguments, the result is a tuple with one item per argument.Without arguments,group1 defaults to zero (the whole matchis returned).If agroupN argument is zero, the corresponding return value is theentire matching string; if it is in the inclusive range [1..99], it isthe string matching the the corresponding parenthesized group. If agroup number is negative or larger than the number of groups definedin the pattern, anIndexError exception is raised.If a group is contained in a part of the pattern that did not match,the corresponding result isNone. If a group is contained in apart of the pattern that matched multiple times, the last match isreturned.

If the regular expression uses the(?P<name>...) syntax,thegroupN arguments may also be strings identifying groups bytheir group name. If a string argument is not used as a group name inthe pattern, anIndexError exception is raised.

A moderately complicated example:

m = re.match(r"(?P<int>\d+)\.(\d*)", '3.14')

After performing this match,m.group(1) is'3', as ism.group('int'), andm.group(2) is'14'.

groups([default])
Return a tuple containing all the subgroups of the match, from 1 up tohowever many groups are in the pattern. Thedefault argument isused for groups that did not participate in the match; it defaults toNone. (Incompatibility note: in the original Python 1.5release, if the tuple was one element long, a string would be returnedinstead. In later versions (from 1.5.1 on), a singleton tuple isreturned in such cases.)

groupdict([default])
Return a dictionary containing all thenamed subgroups of thematch, keyed by the subgroup name. Thedefault argument isused for groups that did not participate in the match; it defaults toNone.

start([group])
end([group])
Return the indices of the start and end of the substringmatched bygroup;group defaults to zero (meaning the wholematched substring).Return-1 ifgroup exists butdid not contribute to the match. For a match objectm, and a groupg that did contribute to the match, thesubstring matched by groupg (equivalent tom.group(g)) is

m.string[m.start(g):m.end(g)]

Note thatm.start(group) will equalm.end(group) ifgroup matched a null string. For example, afterm =re.search('b(c?)', 'cba'),m.start(0) is 1,m.end(0) is 2,m.start(1) andm.end(1) are both 2, andm.start(2) raisesanIndexError exception.

span([group])
ForMatchObjectm, return the 2-tuple(m.start(group),m.end(group)).Note that ifgroup did not contribute to the match, this is(-1, -1). Again,group defaults to zero.

pos
The value ofpos which was passed to thesearch() ormatch() function. This is the indexinto the string at which the RE engine started looking for a match.

endpos
The value ofendpos which was passed to thesearch() ormatch() function. This is the indexinto the string beyond which the RE engine will not go.

lastgroup
The name of the last matched capturing group, orNone if thegroup didn't have a name, or if no group was matched at all.

lastindex
The integer index of the last matched capturing group, orNoneif no group was matched at all.

re
The regular expression object whosematch() orsearch() method produced thisMatchObject instance.

string
The string passed tomatch() orsearch().


Previous PageUp One LevelNext PagePython Library ReferenceContentsModule IndexIndex
Previous:4.2.4 Regular Expression ObjectsUp:4.2 reNext:4.2.6 Examples
Release 2.2.3, documentation updated on 30 May 2003.
SeeAbout this document... for information on suggesting changes.
[8]ページ先頭

©2009-2026 Movatter.jp