recherche

Maison  >  Questions et réponses  >  le corps du texte

Python正则表达式多组匹配

阿神阿神2904 Il y a quelques jours858

répondre à tous(2)je répondrai

  • 黄舟

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

    search的功能就是: 从左到右,去计算是否匹配,如果有匹配,就返回。 即只要找到匹配,就返回了。 所以,最多只会匹配一个, 而不会匹配多个。
    findall可以全部匹配。

    1

    2

    3

    4

    5

    6

    7

    8

    9

    10

    11

    12

    13

    14

    15

    <code>#!/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]

    </code>

    répondre
    0
  • 大家讲道理

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

    应当使用 find_all()

    répondre
    0
  • Annulerrépondre