如何使用Python正则表达式从字符串中提取数据?

以下代码从给定的字符串中提取数据,例如first_id,second_id,category

示例

import re
s = 'TS001B01.JPG'
match = re.match(r'(TS\d+)([A|B])(\d+)\.JPG', s)
first_id = match.group(1)
category = match.group(2)
second_id = match.group(3)
print first_id
print category
print second_id

输出结果

这给出了输出

TS001
B
01