Python Extract Substring From Between Parenthesis
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)
','.join(result)
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?
Answer
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]
print(result)
Output:
['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 ID
s, s
does not starts with (
, s
does not starts with )
, there is at least one character between any two brackets.
Related Questions
- → What are the pluses/minuses of different ways to configure GPIOs on the Beaglebone Black?
- → Django, code inside <script> tag doesn't work in a template
- → React - Django webpack config with dynamic 'output'
- → GAE Python app - Does URL matter for SEO?
- → Put a Rendered Django Template in Json along with some other items
- → session disappears when request is sent from fetch
- → Python Shopify API output formatted datetime string in django template
- → Can't turn off Javascript using Selenium
- → WebDriver click() vs JavaScript click()
- → Shopify app: adding a new shipping address via webhook
- → Shopify + Python library: how to create new shipping address
- → shopify python api: how do add new assets to published theme?
- → Access 'HTTP_X_SHOPIFY_SHOP_API_CALL_LIMIT' with Python Shopify Module