正则匹配
把字符串如下面格式的:
FILE db5f53d8dc0ed510c1b86804a349abc0.MP3: ROCK {Name: MyLove, Mid: 115, Type: unknown}
变成这种格式:
db5f53d8dc0ed510c1b86804a349abc0 ROCK MyLove 115
[解决办法]
>>> string = '''FILE db5f53d8dc0ed510c1b86804a349abc0.MP3: ROCK {Name: MyLove, M
id: 115, Type: unknown}'''
>>> alist = string.split(' ')
>>> ret_str = alist[1][:-5] + ' ' + alist[2] + ' ' + alist[4][:-1] +' ' + alist[
6][:-1]
>>> print ret_str
db5f53d8dc0ed510c1b86804a349abc0 ROCK MyLove 115
>>>
[解决办法]
import re
patt = re.compile(r'''
FILE\s(?P<filename>.*)(?P<extname>\.\S+)\:\s
ROCK\s\{(?P<attrib>.*)\}
''', re.I
[解决办法]
re.U
[解决办法]
re.X)
attrpatt = re.compile(r'''
((?P<key>\w+)\:\s(?P<value>[^\,]+),?)+
''', re.I
[解决办法]
re.U
[解决办法]
re.X)
def parser(str):
m = patt.match(str)
if m:
md = m.groupdict()
sd = dict([g[1:] for g in attrpatt.findall(md['attrib'])])
return md, sd
md, sd = parser('FILE db5f53d8dc0ed510c1b86804a349abc0.MP3: ROCK {Name: MyLove, Mid: 115, Type: unknown}')
print '%(filename)s ROCK %(attribvalues)s' % dict(
filename = md['filename'],
attribvalues = ' '.join([item for item in sd.values() if item and item != 'unknown'])
)
# -*- coding:gb2312 -*-
import re
content = r'FILE db5f53d8dc0ed510c1b86804a349abc0.MP3: ROCK {Name: MyLove, Mid: 115, Type: unknown}'
match = re.search(r'^FILE\s+(\w+)\.\w+:\s+(\w+)\s+\{Name:\s+(\w+),\s+\w+:\s+(\w+)', content, re.I)
print " ".join(match.groups())
db5f53d8dc0ed510c1b86804a349abc0 ROCK MyLove 115