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)
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".
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.
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.
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.
With re.findall()
Example:
s = "-ab-cde-fghi-jkl-mn"
re.findall(r'[a-z]+', s)
Output:
['ab', 'cde', 'fghi', 'jkl', 'mn']
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With