How to extract data from a string with Python Regular Expressions?


The following code extracts data like first_id, second_id, category from given strings

Example

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

Output

This gives output

TS001
B
01

Updated on: 20-Feb-2020

705 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements