Use findAll () Match Method
Description:
findAll () method is used to search for all strings in line with the regular expression in the entire string and returns in the form of a list . If the match is successful. The list contains the matching structure is returned, otherwise it returns an empty list.
re.findall(pattern,string,flags)
Example 1:
import re
pattern=r'mr_\w+'
string='MR_SHOP mr_shop'
match=re.findall(pattern,string,re.I)
print(match)
result
['MR_SHOP', 'mr_shop']
Example 2:
import re
string='项目名称MR_SHOP mr_shop'
match=re.findall(pattern,string,re.I)
print(match)
result
['MR_SHOP', 'mr_shop']
note:
import re
pattern=r'[1-9]{1,3}(\.[0-9]{1,3}){3}'
str1='127.0.0.1 192.168.1.66'
match=re.findall(pattern,str1)
print(match)
result
['.1', '.66']
If the packet contains the specified pattern string text list is returned with the packet matches. (I.e. only match the pattern string in parentheses)
pattern=r'(\.[0-9]{1,3}){3}'
str1='127.0.0.1 192.168.1.66'
match=re.findall(pattern,str1)
print(match)
result:
['.1', '.66']
Here analysis, pattern string (. [0-9] {1,3}) {3} means.
First, [0-9] and \ d have the meaning expressed identical match numbers, {1,3} indicates a match preceding character at least once, up to 3 times. {3} which matches three times the previous character.
Here it is: matching . (Point) begin behind the numbers (0 to 9 may be arbitrary) number and surface may be one or two, up to three. Namely the following situations:
0.05 or 0.66, or .192 and so on.
{3} matches three times, i.e. three consecutive this type, '127.0.0.1 192.168.1.66' can be seen .0.0.1, appeared three times and three times .168.1.66 also consecutive. So the results: [ '1' '66'].
Otherwise if the packet matches the inside need read as follows:
The first written:
the entire pattern string using a pair of parentheses grouping. And returns a list of each element (a tuple) of the first element.
import re
pattern=r'([1-9]{1,3}(\.[0-9]{1,3}){3})'
str1='127.0.0.1 192.168.1.66'
match=re.findall(pattern,str1)
for i in match:
print(i[0])
result:
127.0.0.1
192.168.1.66
The second method:
the original group in plus:? Capture put the group into a non-capturing group.
import re
pattern=r'[1-9]{1,3}(?:\.[0-9]{1,3}){3}'
str1='127.0.0.1 192.168.1.66'
match=re.findall(pattern,str1)
print(match)
['127.0.0.1', '192.168.1.66']
Finished ...