如何在 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 次瀏覽

開啟你的職業生涯

完成課程即可獲得認證

開始學習
廣告