How to name groups in python regular expressions
This article will explain in detail how to name groups in python regular expressions. The editor thinks it is very practical, so I share it for you as a reference. I hope you can get something after reading this article.
1. After naming the group in the regular expression, when the group is accessed in the program, it can be accessed not only through the group number, but also through the group name.
2. Group naming is achieved by adding at the beginning of the group? P.
Example
Import re p = r'(? P\ d {3pm 4})-(? P\ d {7pr 8}) m = re.search (p '010-87654321') print (m) print (m.group ()) # returns the matching string print (m.groups ()) # gets all the group content # returns the group content print (m.group (1)) print (m.group (2)) # returns the group content print (m.group ('area_code')) print (m.group (' phone_code')) through the group name
Once a regular expression is named, it can be accessed by group number or group name.
This is the end of the article on "how to name groups in python regular expressions". I hope the above content can be of some help to you, so that you can learn more knowledge. if you think the article is good, please share it for more people to see.