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: 2020-02-20T05:18:00+05:30

916 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements