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

如何使用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

Python教程

Java教程

Web教程

数据库教程

图形图像教程

大数据教程

开发工具教程

计算机教程