How do I get to *all* of the groups of an re search?
Kyler Laird
Kyler at news.Lairds.org
Sat Jan 11 18:53:37 EST 2003
Andrew Dalke <adalke at mindspring.com> writes:
>>>Sure. For example, finding the last component of a path expression, in
>>>order to isolate the file name, is a common application.
>> That's readily done using simple REs without depending on this
>> behavior.
>The simple re is
> >>> import re
> >>> pat = re.compile(r"(/([^/]+)*)+")
> >>> pat.match("/home/dalke/tmp/whatever.txt").group(2)
>'whatever.txt'
What's the problem with something simpler like this?
/([^/]+)$
>If I understand you rightly, you would want to use the same
>re to get all the filename matches, eg, so that 'allmatches(2)'\
>(or somesuch) would return ['home', 'dalke', 'tmp', 'whatever.txt']
That'd be great, yes.
>Again, I see your point in that the interface to regexp results
>isn't as powerful as it could be. I still think your proposed
>inteface isn't itself powerful enough.
It would satisfy my immediate need and make the statement "the
contents of a group can be retrieved after a match has been
performed" true.
>Now in your case, you want to advocate Python for people who
>haven't learned other languages. In other words, for people
>who are not programmers. This is a new criteria, since you
>hadn't mentioned it before.
I want the documentation, each piece of it, to stand on its
own. That does not change simply because the reason I am most
concerned about it is primarily for the benefit of new users.
>With that additional information, I'll suggest that regular
>expressions are not for the faint of heart nor for just about
>every new programmer.
O.k., in that case it's fine to have misleading information in
the documentation. It'll be a "rite of passage" for Python
programmers to stumble over such things. 'should keep out the
riff-raff.
--kyler
More information about the Python-list
mailing list