黄舟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]