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

pythonserver side programmingprogramming更新于 2023/11/10 3:36:00

以下代码从给定的字符串中提取 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

输出

This gives output

TS001
B
01

相关文章