Home  >  Q&A  >  body text

Python正则表达式多组匹配

阿神阿神2761 days ago801

reply all(2)I'll reply

  • 黄舟

    黄舟2017-04-17 15:16:00

    The function of search is: from left to right, calculate whether there is a match, and if there is a match, return it. That is, as long as a match is found, it is returned. Therefore, at most one will be matched, not multiple.
    findall can match all.

    #!/usr/bin/python
    # -*- coding: utf-8 -*- 
    
    import re
    
    str = 'xiaohong loves xiaoming,xiaozhu loves xiaoli,xiaopeng loves xiaozhao'
    
    names = re.findall(r'(\S+) loves (\S+)(,|$)',str, re.I)
    
    print names
    
    if names:
        for group in names:
            print group[0], group[1]
    

    reply
    0
  • 大家讲道理

    大家讲道理2017-04-17 15:16:00

    should use find_all()

    reply
    0
  • Cancelreply