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

PythonServer Side ProgrammingProgramming

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
raja
Published on 10-Jan-2018 11:37:33
Advertisements