In [60]: re.findall('(?m)\s+(?!noreply|postmaster)(\w+)',
"""
sales@phptr.com
postmaster@phptr.com
eng@phptr.com
noreply@phptr.com
"""
)
Out[60]: ['sales', 'eng']
In [61]: re.findall('(?m)\s+',
"""
sales@phptr.com
postmaster@phptr.com
eng@phptr.com
noreply@phptr.com
"""
)
Out[61]: ['\n ', '\n ', '\n', '\n', '\n']
In [62]: re.findall('(?m)\s+(\w+)',
"""
sales@phptr.com
postmaster@phptr.com
eng@phptr.com
noreply@phptr.com
"""
)
Out[62]: ['sales', 'postmaster', 'eng', 'noreply']
为什么第一、三次的空格和换行没有匹配,而第二次的匹配了?
findall returns the grouped content first
(w+)
Without grouping, the content matched by the entire expression is returned~
s+
(?m)s+(?!noreply|postmaster)(w+) This requires that the matched string (space and newline character) must be followed by noreply or postmaster and then followed by 1 or more words (not @ like this symbol, @ will not be matched by w).
I feel that the problem should be in python’s findall function. For example, if you change the first one to this:
It seems that spaces can be matched. You can go find some principles of python findall and take a look
zilong_thu's answer is wrong, (
?!
noreply|postmaster) means that the matching is successful only if the following content does not match. The second one can match spaces because s matches any invisible characters, including spaces, tabs, form feeds, etc. Equivalent to [fnrtv]. The reason for the absence of 1 and 3 is that the content of the group is actually returned, that is, the content in parentheses. If there are multiple groups, they will be formed into a tuple, and then a list will be returned