9 Python Regex Group Functions
9 Python Regex Group Functions
Python Regex group() function explained with examples: named groups and groupdict.
A regular expression can have named groups. This makes it easier to retrieve
those groups after calling match() . But it makes the pattern more complex.
#!/usr/bin/python
import re
# A string.
name = "Learn Scientific"
We can get the first name with the string “first” and the group() method. We
use “last” for the last name.
import re
# Match names.
m = re.match("(?P<first>\w+)\W+(?P<last>\w+)", name)
if m:
# Get dict.
d = m.groupdict()