Python split string by start and end characters
Solution 1
I would do it like this:
"(hello) (yes) (yo diddly)"[1:-1].split(") (")
First, we cut off the first and last characters (since they should be removed anyway). Next, we split the resulting string using ") (" as the delimiter, giving the desired list.
Solution 2
import re
pattern = re.compile(r'\(([^)]*)\)')
The pattern matches the parentheses in your string (\(...\)
) and these need to be escaped.
Then it defines a subgroup ((...)
) - these parentheses are part of the regex-syntax.
The subgroup matches all characters except a right parenthesis ([^)]*
)
s = "(hello) (yes) (yo diddly)"
pattern.findall(s)
gives
['hello', 'yes', 'yo diddly']
UPDATE:
It is probably better to use [^)]+
instead of [^)]*
. The latter would also match an empty string.
Using the non-greedy modifiers, as DSM suggested, makes the pattern probably better to read: pattern = re.compile(r'\((.+?)\)')
Solution 3
This will give you words from any string :
>>> s="(hello) (yes) (yo diddly)"
>>> import re
>>> words = re.findall(r'\((.*?\))',s)
>>> words
['hello', 'yes', 'yo diddly']
as D.S.M said.
?
in the regex to make it non-greedy.
Comments
-
Name McChange almost 2 years
Say you have a string like this:
"(hello) (yes) (yo diddly)"
.You want a list like this:
["hello", "yes", "yo diddly"]
How would you do this with Python?
-
Name McChange over 11 yearsI've got to wait 6 minutes, just hold on ;)
-
arshajii over 11 yearsI think he would want "yo diddly" as one entry in the array.
-
vivek over 11 years@DSM you dont need the inner pair of parenthesis.
-
DSM over 11 years@vivek: but if you don't use them, then you get left with parentheses. Compare your output to the desired output.
-
Roland Smith over 11 yearsThe answer using regular expressions is more flexible. This answer would not produce the correct output e.g. if the string started or ended with a space, or if there was more than one space or a tab character between the parentheses.
-
DSM over 11 yearsDoes this have advantages over using
?
? -
arshajii over 11 yearsVery true. I purposefully avoided regular expressions in this answer because I thought a solution could be achieved through simpler means. Of course, the OP can use them if he requires further flexibility like you mentioned.
-
tzelleke over 11 years@DSM I don't know - but probably its clearer to use the non-greedy syntax. I'll update my answer this way