2 回答

TA贡献1877条经验 获得超1个赞
我正在使用命名组,因为这样可以更轻松地引用它们:
import re
pattern = r"""
\b(?P<first_part>\w*(?P<splitter>\w)) # matches starting at a word boundary
(?P<last_part>(?P=splitter)er\b) # matches the last letter of the first group
# plus 'er' if followed by a word boundary
"""
matcher = re.compile(pattern, re.X)
print matcher.search('letter').groupdict()
# out: {'first_part': 'let', 'last_part': 'ter', 'splitter': 't'}

TA贡献1829条经验 获得超13个赞
我希望第一组是所有事物,直到并包括两个相同符号中的第一个,第二组是第二个相同符号后跟“ er”
那将是:
match = re.search(r'(\w*(\w)(?=\2))(\w*er$)', str)
print match.groups()
# -> ('Let', 't', 'ter')
添加回答
举报