The beginning and knot of matching strings in packet regularities in 42 python
The part of a regular expression enclosed in a pair of parentheses is called a grouping #'(\ d\ d\ d)-(\ d\ d) 'import re'''1. Only the parts enclosed by parentheses are considered as a group. If there are both parts enclosed by parentheses and parts not enclosed by parentheses in a regular expression, then only the parts enclosed in parentheses are counted as a group. 2. The group method, if you do not specify a parameter, returns the matching entire string, and if you add a parameter, it returns the string of the specified grouping, and the group index starts at 1. Groups method Returns all matching groups 4. 5 in tuples. The index of the grouping is from 1, divided into 3 groups of 3 digits-4 digits-2 lowercase letters m = re.match ('(\ d {3}) -\ d {4}-[a murz] {2}', '123-4567) print (m) # print (m.groups ()) # (' 123') ) print (m.group ()) # 123-4567-xyprint (m.group (1)) # 123print ('-') m = re.match ('(\ d {3})-(\ d {4}))-[amurz] {2}', '123-4567 m.groups') print (m) # print (m.groups ()) # ('123') This returns a tuple print (m.group ()) # 123-4567-xyprint (m.group (1)) # 123print (m.group (2)) # 456print ('-') m = re.match ('(\ d {3})-(\ d {4}))-([amurz] {2})'as long as it is in a group Print (m) # print (m.groups ()) # ('123 houses,' 4567') 'xy') print (m.group ()) # 123-4567-xyprint (m.group (1)) # 123print (m.group (2)) # 456print (m.group (3)) # xyprint (' -') m = re.match ('(\ d {3} -\ d {4})-([Amurz] {2})' Print (m) # print (m.groups ()) # 'xy') print (m.group ()) # 123-4567-xyprint (m.group (1)) # 123-4567print (m.group (2)) # xy lesson 8 the beginning and end of the matching string and the word boundary # the beginning and end of the matching string and the word boundary''^': the beginning of the matching string "$": the end of the matching string\ b Match word boundaries: refers to spaces or punctuation marks "hello?" There is a boundary on the left and right sides. "world0" means there is a boundary on the left side. There is no boundary on the right''import re# "The" must be at the beginning of the string to find m = re.search (' The', 'abc The.') print (m) # m = re.search (' ^ The', 'The bus.') print (m) # # The must be at the end of the string to search for m = re.search (' The$','The end.') print (m) # Nonem = re.search ('The$') 'end.The') print (m) # print ("-") # requires that there must be a boundary on the left side of this' to find m = re.search (r'\ bthis', "What's this?") # if you don't use the\ b in r, you will be escaped print (m) # m = re.search (r'\ bthis') "this is a bus.") print (m) # m = re.search (r'\ bthis', "is a bus.") print (m) # m = re.search (r'\ bthis', "1234this is a bus.") print (m) # Nonem = re.search (r'\ bthis\ b') "What's this123") print (m) # None lesson 9 regular use findall and finditer functions to find all matching results # use findall and finditer functions to find all strings matching regular expressions' findall: returns all strings that meet the criteria through a list DOM this returns all the results together finditer: returns the search results through an iterator SAX this is not all returns''import res =' 12-a-abc54-a-xyz---78-A-ytr'result = re.findall (r'\ d\ d-[a]-[Amurz] {3}', s) print (result) # ['12Mel'aMel ABC' Result = re.findall (r'\ d\ d-[aA]-[a murz] {3}', s) # the [aA] in this step also ignores the uppercase and lowercase print (result) # ['12Muchal Abcession,' 54muraLexyz' Result = re.findall (r'(\ d\ d-[aA])-([Amurz] {3})', s) # the grouping is returned and split into a group print (result) # [('12mura,' abc'), ('54mura,' xyz'), ('78mura') 'ytr')] S1 =' 12-a-abc54-a-xYz---78-A-ytr'result = re.findall (r'\ d\ dmura-[amurz] {3}', S1 meme re.I) # you can add a third parameter, and the position of the third parameter can ignore the uppercase and lowercase re.I. This is to ignore the uppercase and lowercase print (result) # ['12MuthaMuthabc' It = re.finditer (r'(\ d\ d)-a-([Amurz] {3})', for result in it: print (result.group (), end='') '12-a-abc 54-a-xyz 78'.