Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Capturing group with findall?

Tags:

python

regex

How can I access captured groups if I do findall(r'regex(with)capturing.goes.here') ? I know I can do it through finditer, but I don't want to iterate.

like image 586
Pablo Avatar asked May 16 '11 13:05

Pablo


People also ask

What is capturing group regex?

Capturing groups are a way to treat multiple characters as a single unit. They are created by placing the characters to be grouped inside a set of parentheses. For example, the regular expression (dog) creates a single group containing the letters "d" "o" and "g" .

What is capturing group in regex Python?

Capturing groups are a handy feature of regular expression matching that allows us to query the Match object to find out the part of the string that matched against a particular part of the regular expression. Anything you have in parentheses () will be a capture group.

How do I reference a capture group in regex?

If your regular expression has named capturing groups, then you should use named backreferences to them in the replacement text. The regex (?' name'group) has one group called “name”. You can reference this group with ${name} in the JGsoft applications, Delphi, .

What is capturing group in regex Javascript?

A part of a pattern can be enclosed in parentheses (...) . This is called a “capturing group”. That has two effects: It allows to get a part of the match as a separate item in the result array.


2 Answers

findall just returns the captured groups:

>>> re.findall('abc(de)fg(123)', 'abcdefg123 and again abcdefg123') [('de', '123'), ('de', '123')] 

Relevant doc excerpt:

Return all non-overlapping matches of pattern in string, as a list of strings. The string is scanned left-to-right, and matches are returned in the order found. If one or more groups are present in the pattern, return a list of groups; this will be a list of tuples if the pattern has more than one group. Empty matches are included in the result unless they touch the beginning of another match.

like image 153
Eli Bendersky Avatar answered Oct 19 '22 04:10

Eli Bendersky


Use groups freely. The matches will be returned as a list of group-tuples:

>>> re.findall('(1(23))45', '12345') [('123', '23')] 

If you want the full match to be included, just enclose the entire regex in a group:

>>> re.findall('(1(23)45)', '12345') [('12345', '23')] 
like image 29
bluepnume Avatar answered Oct 19 '22 04:10

bluepnume