Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python regexp groups: how do I get all groups?

Tags:

python

regex

I am puzzled by this:

>>> import re
>>> re.match(r"(?P<all>-(?P<one>\w+))*","-ab-cde-fghi-jkl-mn").groups()
('-mn', 'mn')
>>> re.match(r"(?P<all>-(?P<one>\w+)*)","-ab-cde-fghi-jkl-mn").groups()
('-ab', 'ab')

How do I get the list of all terms, ideally like

["ab","cde","fghi","jkl","mn"]

but

"-ab-cde-fghi-jkl-mn"

is fine too.

(Please note that I am fully aware of str.split("-"). This is a question about re - how to match the whole set)

like image 322
sds Avatar asked Oct 20 '16 14:10

sds


People also ask

How do I capture a group in 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 group () in Python?

groups() method. This method returns a tuple containing all the subgroups of the match, from 1 up to however many groups are in the pattern. The default argument is used for groups that did not participate in the match; it defaults to None. In later versions (from 1.5.

How do you replace all occurrences of a regex pattern in a string in Python?

sub() method will replace all pattern occurrences in the target string. By setting the count=1 inside a re. sub() we can replace only the first occurrence of a pattern in the target string with another string. Set the count value to the number of replacements you want to perform.

What is regex match group?

Regular expressions allow us to not just match text but also to extract information for further processing. This is done by defining groups of characters and capturing them using the special parentheses ( and ) metacharacters. Any subpattern inside a pair of parentheses will be captured as a group.


1 Answers

With re.findall()

Example:

s = "-ab-cde-fghi-jkl-mn"
re.findall(r'[a-z]+', s)

Output:

['ab', 'cde', 'fghi', 'jkl', 'mn']
like image 159
dgg32 Avatar answered Oct 05 '22 23:10

dgg32