python正则匹配两个字符之间的内容(转自:小晓酱手记)

提取指定字符串的关键信息。具体要求是从一个字符串中提取<>里面的内容。

输入:我要听<梁博><男孩>
输出:梁博 男孩
 Python 实现: 

#!/usr/bin/python3
#coding:utf8
import re
import sys
reload(sys)
sys.setdefaultencoding('utf8')
 
template = "我要听<歌手名>的<歌曲名>"
 
 
def subString(template):
    rule = r'<(.*?)>' # 正则规则
    slotList = re.findall(rule, template)
    return slotList
 
slotList = subString(template)
for slot in slotList:
    print slot


原文地址:Python 提取指定中间字符串 取出字符串中间文本,利用正则表达式_给我一点温度-CSDN博客