472,126 Members | 1,596 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,126 software developers and data experts.

Re: how to get all repeated group with regular expression

Steve Holden wrote:
Please keep this on the list.

scsoce wrote:
>Steve Holden wrote:
>>scsoce wrote:

say, when I try to search and match every char from variable length
string, such as string '123456', i tried re.findall( r'(\d)*, '12346' )

I think you will find you missed a quote out there. Always better to
copy and paste ...
, but only get '6' and Python doc indeed say: "If a group is contained
in a part of the pattern that matched multiple times, the last match is
returned."

So use

r'(\d*)'

instead and then the group includes all the digits you match.
cause the regx engine cannot remember all the past history then ? is it
nature to all regx engine or only to Python ?

Different regex engines have different capabilities, so I can't speak to
them all. If you wanted *all* the matches of *all* groups, how would you
have them returned? As a list? That would make the case where there was
only one match much tricker to handle. And what would you do with

r'((\w)*\d)*)'

Also, what about named groups? I can see enough potential implementation
issues that I can perfectly understand why Python works the way it does,
so I'd be interested to know why it doesn't makes sense to you, and what
you would prefer it to do.

regards
Steve
maybe my expression was not clear. I want to capture every matched part
in a repeated pattern, not only the last, say, for string '123456', I
want to back reference any one char, not only the '6'. and i know the
example is very simple, so we can got the whole string using regx and
get every char using other python statements, but if the pattern in
group is complex?
and I test in VIM, it can do the 'back reference':
==you text in vim:
123456
== pattern:
:%s/\(\d\)*/$2
text will turn to be:
2
'Fraid the Python re implementers just decided not to do it that way.
Nor Perl.

Probably what you want is re.findall(r"(\d)", "123456"), which returns a
list of what it captured.

Nov 21 '08 #1
0 3416

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

2 posts views Thread by Bryce Budd | last post: by
4 posts views Thread by Neri | last post: by
11 posts views Thread by Dimitris Georgakopuolos | last post: by
7 posts views Thread by Billa | last post: by
7 posts views Thread by matteosartori | last post: by
25 posts views Thread by Mike | last post: by
2 posts views Thread by Tedmond | last post: by
7 posts views Thread by hellbent4u | last post: by
reply views Thread by M.-A. Lemburg | last post: by
reply views Thread by leo001 | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.