Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #3796
| References | <4daf31e3$0$10596$742ec2ed@news.sonic.net> |
|---|---|
| Date | 2011-04-21 15:57 +0200 |
| Subject | Re: Groups in regular expressions don't repeat as expected |
| From | Vlastimil Brom <vlastimil.brom@gmail.com> |
| Newsgroups | comp.lang.python |
| Message-ID | <mailman.701.1303394244.9059.python-list@python.org> (permalink) |
2011/4/20 John Nagle <nagle@animats.com>:
> Here's something that surprised me about Python regular expressions.
>
>>>> krex = re.compile(r"^([a-z])+$")
>>>> s = "abcdef"
>>>> ms = krex.match(s)
>>>> ms.groups()
> ('f',)
>
>...
> "If a group is contained in a part of the pattern that matched multiple
> times, the last match is returned."
>
> That's kind of lame, though. I'd expect that there would be some way
> to retrieve all matches.
>
> John Nagle
> --
> http://mail.python.org/mailman/listinfo/python-list
Hi,
do you mean something like:
>>> import regex
>>> ms = regex.match(r"^([a-z])+$", "abcdef")
>>> ms.captures(1)
['a', 'b', 'c', 'd', 'e', 'f']
>>>
>>> help(ms.captures)
Help on built-in function captures:
captures(...)
captures([group1, ...]) --> list of strings or tuple of list of strings.
Return the captures of one or more subgroups of the match. If there is a
single argument, the result is a list of strings; if there are multiple
arguments, the result is a tuple of lists with one item per argument; if
there are no arguments, the captures of the whole match is returned. Group
0 is the whole match.
>>>
cf.
http://pypi.python.org/pypi/regex
hth,
vbr
Back to comp.lang.python | Previous | Next — Previous in thread | Next in thread | Find similar
Groups in regular expressions don't repeat as expected John Nagle <nagle@animats.com> - 2011-04-20 12:20 -0700
Re: Groups in regular expressions don't repeat as expected Neil Cerutti <neilc@norwich.edu> - 2011-04-20 19:23 +0000
Re: Groups in regular expressions don't repeat as expected John Nagle <nagle@animats.com> - 2011-04-20 13:34 -0700
Re: Groups in regular expressions don't repeat as expected Neil Cerutti <neilc@norwich.edu> - 2011-04-21 13:16 +0000
Re: Groups in regular expressions don't repeat as expected John Nagle <nagle@animats.com> - 2011-04-24 12:43 -0700
Re: Groups in regular expressions don't repeat as expected MRAB <python@mrabarnett.plus.com> - 2011-04-20 21:03 +0100
Re: Groups in regular expressions don't repeat as expected Vlastimil Brom <vlastimil.brom@gmail.com> - 2011-04-21 15:57 +0200
Re: Groups in regular expressions don't repeat as expected Vlastimil Brom <vlastimil.brom@gmail.com> - 2011-04-21 20:36 +0200
csiph-web