search

Home  >  Q&A  >  body text

python - 有关正则表达式的问题

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']

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

大家讲道理大家讲道理2913 days ago219

reply all(4)I'll reply

  • 黄舟

    黄舟2017-04-18 10:15:03

    findall returns the grouped content first (w+)

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

    reply
    0
  • PHP中文网

    PHP中文网2017-04-18 10:15:03

    (?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).

    reply
    0
  • 大家讲道理

    大家讲道理2017-04-18 10:15:03

    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

    reply
    0
  • PHP中文网

    PHP中文网2017-04-18 10:15:03

    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

    reply
    0
  • Cancelreply