I have a string "Name(something)" and I am trying to extract the portion of the string within the parentheses!
Iv'e tried the following solutions but don't seem to be getting the results I'm looking for.
n.split('()')
name, something = n.split('()')
You can look for
(
and)
(need to escape these using backslash in regex) and then match every character using.*
(capturing this in a group).Example:
Outputs:
Without regex you can do the following:
Outputs:
as an improvement on @Maroun Maroun 's answer:
it finds all instances of strings in between parentheses
You can use
re.match
:that matches two
(.*)
which means anything, where the second is between parentheses\(
&\)
.You can use a simple regex to catch everything between the parenthesis:
The regex matches the first "(", then it matches everything that's not a ")":
\(
matches the character "(" literally([^)]+)
greedily matches anything that's not a ")"You can use split as in your example but this way
or using regex