Python Extract Substring From Between Parenthesis

- 1 answer

I have a string, that is formatted like that:

"Name Surname (ID), Name2 Surname2 (ID2)"

ID starts with letter that is followed by few digits. We can have various number of people in that string (I mean there can be only one person, 2 as in provided example or even more). Also, people can have few names or surnames, so it's not consistent.

I want to extract a substring built of ID's divided by colons, so for this example it would look like that:

"ID, ID2"

Right now i tried this approach:

import re

string = "Bob Rob Smith (L1234567), John Doe (k12345678)"
result = re.findall(r'[a-zA-Z][0-9]+', string)

And it works perfectly fine, but I wonder if there's simpler approach that doesn't require any additional modules. Do you guys have any ideas?



I also think using re is good approach, if you have to NOT use re AT ANY PRICE, then you might do:

s = "Bob Rob Smith (L1234567), John Doe (k12345678)"
result = s.replace(')','(').split('(')[1::2]


['L1234567', 'k12345678']

Explanation: I want to split at ( and ), but .split method of str accepts only one delimiter, so I firstly replace ) with (, then I split and get odd elements. This method will work if: ( and ) are used solely around IDs, s does not starts with (, s does not starts with ), there is at least one character between any two brackets.