如何使用Python正则表达式从字符串中提取数据?
以下代码从给定字符串中提取数据,例如first_id、second_id、category。
阅读更多:Python 教程
示例
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