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
605bdae0
Kaydet (Commit)
605bdae0
authored
Eyl 11, 2016
tarafından
Eric V. Smith
Dosyalara gözat
Seçenekler
Dosyalara Gözat
İndir
Eposta Yamaları
Sade Fark
Issue 24454: Improve the usability of the re match object named group API
üst
a3c1728b
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
82 additions
and
1 deletion
+82
-1
re.rst
Doc/library/re.rst
+16
-0
test_re.py
Lib/test/test_re.py
+44
-0
NEWS
Misc/NEWS
+4
-0
_sre.c
Modules/_sre.c
+18
-1
No files found.
Doc/library/re.rst
Dosyayı görüntüle @
605bdae0
...
...
@@ -1015,6 +1015,22 @@ Match objects support the following methods and attributes:
'c3'
.. method:: match.__getitem__(g)
This is identical to ``m.group(g)``. This allows easier access to
an individual group from a match:
>>> m = re.match(r"(\w+) (\w+)", "Isaac Newton, physicist")
>>> m[0] # The entire match
'Isaac Newton'
>>> m[1] # The first parenthesized subgroup.
'Isaac'
>>> m[2] # The second parenthesized subgroup.
'Newton'
.. versionadded:: 3.6
.. method:: match.groups(default=None)
Return a tuple containing all the subgroups of the match, from 1 up to however
...
...
Lib/test/test_re.py
Dosyayı görüntüle @
605bdae0
...
...
@@ -441,6 +441,50 @@ class ReTests(unittest.TestCase):
self
.
assertEqual
(
m
.
group
(
2
,
1
),
(
'b'
,
'a'
))
self
.
assertEqual
(
m
.
group
(
Index
(
2
),
Index
(
1
)),
(
'b'
,
'a'
))
def
test_match_getitem
(
self
):
pat
=
re
.
compile
(
'(?:(?P<a1>a)|(?P<b2>b))(?P<c3>c)?'
)
m
=
pat
.
match
(
'a'
)
self
.
assertEqual
(
m
[
'a1'
],
'a'
)
self
.
assertEqual
(
m
[
'b2'
],
None
)
self
.
assertEqual
(
m
[
'c3'
],
None
)
self
.
assertEqual
(
'a1={a1} b2={b2} c3={c3}'
.
format_map
(
m
),
'a1=a b2=None c3=None'
)
self
.
assertEqual
(
m
[
0
],
'a'
)
self
.
assertEqual
(
m
[
1
],
'a'
)
self
.
assertEqual
(
m
[
2
],
None
)
self
.
assertEqual
(
m
[
3
],
None
)
with
self
.
assertRaisesRegex
(
IndexError
,
'no such group'
):
m
[
'X'
]
with
self
.
assertRaisesRegex
(
IndexError
,
'no such group'
):
m
[
-
1
]
with
self
.
assertRaisesRegex
(
IndexError
,
'no such group'
):
m
[
4
]
with
self
.
assertRaisesRegex
(
IndexError
,
'no such group'
):
m
[
0
,
1
]
with
self
.
assertRaisesRegex
(
IndexError
,
'no such group'
):
m
[(
0
,)]
with
self
.
assertRaisesRegex
(
IndexError
,
'no such group'
):
m
[(
0
,
1
)]
with
self
.
assertRaisesRegex
(
KeyError
,
'a2'
):
'a1={a2}'
.
format_map
(
m
)
m
=
pat
.
match
(
'ac'
)
self
.
assertEqual
(
m
[
'a1'
],
'a'
)
self
.
assertEqual
(
m
[
'b2'
],
None
)
self
.
assertEqual
(
m
[
'c3'
],
'c'
)
self
.
assertEqual
(
'a1={a1} b2={b2} c3={c3}'
.
format_map
(
m
),
'a1=a b2=None c3=c'
)
self
.
assertEqual
(
m
[
0
],
'ac'
)
self
.
assertEqual
(
m
[
1
],
'a'
)
self
.
assertEqual
(
m
[
2
],
None
)
self
.
assertEqual
(
m
[
3
],
'c'
)
# Cannot assign.
with
self
.
assertRaises
(
TypeError
):
m
[
0
]
=
1
# No len().
self
.
assertRaises
(
TypeError
,
len
,
m
)
def
test_re_fullmatch
(
self
):
# Issue 16203: Proposal: add re.fullmatch() method.
self
.
assertEqual
(
re
.
fullmatch
(
r"a"
,
"a"
)
.
span
(),
(
0
,
1
))
...
...
Misc/NEWS
Dosyayı görüntüle @
605bdae0
...
...
@@ -143,6 +143,10 @@ Core and Builtins
Library
-------
-
Issue
#
24454
:
Regular
expression
match
object
groups
are
now
accessible
using
__getitem__
.
"mo[x]"
is
equivalent
to
"mo.group(x)"
.
-
Issue
#
10740
:
sqlite3
no
longer
implicitly
commit
an
open
transaction
before
DDL
statements
.
...
...
Modules/_sre.c
Dosyayı görüntüle @
605bdae0
...
...
@@ -2121,6 +2121,12 @@ match_group(MatchObject* self, PyObject* args)
return
result
;
}
static
PyObject
*
match_getitem
(
MatchObject
*
self
,
PyObject
*
name
)
{
return
match_getslice
(
self
,
name
,
Py_None
);
}
/*[clinic input]
_sre.SRE_Match.groups
...
...
@@ -2416,6 +2422,9 @@ PyDoc_STRVAR(match_group_doc,
Return subgroup(s) of the match by indices or names.
\n
\
For 0 returns the entire match."
);
PyDoc_STRVAR
(
match_getitem_doc
,
"__getitem__(name) <==> group(name).
\n
"
);
static
PyObject
*
match_lastindex_get
(
MatchObject
*
self
)
{
...
...
@@ -2706,6 +2715,13 @@ static PyTypeObject Pattern_Type = {
pattern_getset
,
/* tp_getset */
};
/* Match objects do not support length or assignment, but do support
__getitem__. */
static
PyMappingMethods
match_as_mapping
=
{
NULL
,
(
binaryfunc
)
match_getitem
,
NULL
};
static
PyMethodDef
match_methods
[]
=
{
{
"group"
,
(
PyCFunction
)
match_group
,
METH_VARARGS
,
match_group_doc
},
...
...
@@ -2717,6 +2733,7 @@ static PyMethodDef match_methods[] = {
_SRE_SRE_MATCH_EXPAND_METHODDEF
_SRE_SRE_MATCH___COPY___METHODDEF
_SRE_SRE_MATCH___DEEPCOPY___METHODDEF
{
"__getitem__"
,
(
PyCFunction
)
match_getitem
,
METH_O
|
METH_COEXIST
,
match_getitem_doc
},
{
NULL
,
NULL
}
};
...
...
@@ -2751,7 +2768,7 @@ static PyTypeObject Match_Type = {
(
reprfunc
)
match_repr
,
/* tp_repr */
0
,
/* tp_as_number */
0
,
/* tp_as_sequence */
0
,
/* tp_as_mapping */
&
match_as_mapping
,
/* tp_as_mapping */
0
,
/* tp_hash */
0
,
/* tp_call */
0
,
/* tp_str */
...
...
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