如何在 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

更新于:20-Feb-2020

856 次浏览

开启你的职业生涯

完成课程即可获得认证

开始学习
广告
© . All rights reserved.