Capturing named groups in regex with re.findall

随声附和 提交于 2019-12-19 03:39:15

问题


When I was trying to answer this question: regex to split %ages and values in python I noticed that I had to re-order the groups from the result of findall. For example:

data = """34% passed 23% failed 46% deferred"""
result = {key:value for value, key in re.findall('(\w+)%\s(\w+)', data)}
print(result)
>>> {'failed': '23', 'passed': '34', 'deferred': '46'}

Here the result of the findall is:

>>> re.findall('(\w+)%\s(\w+)', data)
>>> [('34', 'passed'), ('23', 'failed'), ('46', 'deferred')]

Is there a way to change/specify the order of the groups that makes re.findall return:

[('passed', '34'), ('failed', '23'), ('deferred', '46')]

Just to clarify, the question is:

Is it possible to specfic the order or re-order the groups for the return of the re.findall function?

I used the example above to create a dictionary to provide a reason/use case for when you would want to change the order (making key as value and value as key)

Further clarification:

In order to handle groups in larger more complicated regexes, you can name groups, but those names are only accessible when you do a re.search pr re.match. From what I have read, findall has a fixed indices for groups returned in the tuple, The question is anyone know how those indices could be modified. This would help make handling of groups easier and intuitive.


回答1:


Take 3, based on a further clarification of the OP's intent in this comment.

Ashwin is correct that findall does not preserve named capture groups (e.g. (?P<name>regex)). finditer to the rescue! It returns the individual match objects one-by-one. Simple example:

data = """34% passed 23% failed 46% deferred"""
for m in re.finditer('(?P<percentage>\w+)%\s(?P<word>\w+)', data):
    print( m.group('percentage'), m.group('word') )



回答2:


Per the OP's comment on my first answer: If you are simply trying to reorder a list of 2-tuples like this:

[('34', 'passed'), ('23', 'failed'), ('46', 'deferred')]

... to look like this, with individual elements reversed:

[('passed', '34'), ('failed', '23'), ('deferred', '46')]

There's an easy solution: use a list comprehension with the slicing syntax sequence[::-1] to reverse the order of the elements of the individual tuples:

a = [('34', 'passed'), ('23', 'failed'), ('46', 'deferred')]
b = [x[::-1] for x in a]
print b



回答3:


As you've identified in your second example, re.findall returns the groups in the original order.

The problem is that the standard Python dict type does not preserve the order of keys in any way. Here's the manual for Python 2.x, which makes it explicit, but it's still true in Python 3.x: https://docs.python.org/2/library/stdtypes.html#dict.items

What you should use instead is collections.OrderedDict:

from collections import OrderedDict as odict

data = """34% passed 23% failed 46% deferred"""
result = odict((key,value) for value, key in re.findall('(\w+)%\s(\w+)', data))
print(result)
>>> OrderedDict([('passed', '34'), ('failed', '23'), ('deferred', '46')])

Notice that you must use the pairwise constructor form (dict((k,v) for k,v in ...) rather than the dict comprehension constructor ({k:v for k,v in ...}). That's because the latter constructs instances of dicttype, which cannot be converted to OrderedDict without losing the order of the keys... which is of course what you are trying to preserve in the first place.



来源:https://stackoverflow.com/questions/25628973/capturing-named-groups-in-regex-with-re-findall

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!