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