python - 有关正则表达式的问题
大家讲道理
大家讲道理 2017-04-18 10:13:03
0
4
211
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']

为什么第一、三次的空格和换行没有匹配,而第二次的匹配了?

大家讲道理
大家讲道理

光阴似箭催人老,日月如移越少年。

reply all(4)
黄舟

findall returns the grouped content first (w+)

Without grouping, the content matched by the entire expression is returned~ s+

Ty80

(?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:

re.findall('(?m)\s+(?!noreply|postmaster)\w+',
"""
    sales@phptr.com
    postmaster@phptr.com
eng@phptr.com
noreply@phptr.com
"""
)

It seems that spaces can be matched. You can go find some principles of python findall and take a look

Ty80

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

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template