Python中提取指定字符串取出中间⽂本正则表达式Python中提取指定字符串,从⼀个字符串中提取<>⾥⾯的内容,整理了两种实现⽅式,后续有更多的实现⽅式继续更新
代码如下:
#coding:utf8
import re
import sys
reload(sys)
sys.setdefaultencoding('utf8')
#!/usr/bin/python
template = "我要<;歌⼿名>的<;歌曲名>"
def subString1(template):
copy = False
finished = False
slotList = []
str = ""
for s in template:
if s=='<':
copy = True
正则表达式提取中文elif s=='>':
copy = False
finished = True
elif copy:
str = str+s
if finished:
slotList.append(str)
str = ""
finished = False
return slotList
def subString2(template):
rule = r'<(.*?)>'
slotList = re.findall(rule, template)
return slotList
slotList = subString1(template)
for slot in slotList:
print slot
slotList = subString2(template)
for slot in slotList:
print slot
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论