I am trying to split strings containing python functions, so that the resulting output keeps separate functions as list elements.
s='hello()there()'
should be split into ['hello()', 'there()']
To do so I use a regex lookahead to split on the closing paranthesis, but not at the end of the string.
While the lookahead seems to work, I cannot keep the )
in the resulting strings as suggested in various posts. Simply splitting with the regex discards the separator:
import re
s='hello()there()'
t=re.split("\)(?!$)", s)
This results in: 'hello(', 'there()']
.
s='hello()there()'
t=re.split("(\))(?!$)", s)
Wrapping the separator as a group results in the )
being retained as a separate element: ['hello(', ')', 'there()']
As does this approach using the filter()
function:
s='hello()there()'
u = list(filter(None, re.split("(\))(?!$)", s)))
resulting again in the paranthesis as a separate element: ['hello(', ')', 'there()']
How can I split such a string so that the functions remain intact in the output?
CodePudding user response:
Use re.findall()
\w \(\)
matches one or more word characters followed by an opening and a closing parenthesis> That part matches thehello()
andthere()
t = re.findall(r"\w \(\)", s)
['hello()', 'there()']
Edition:
s = "hello(x, ...)there(6L)now()"
t = re.findall(r"\w \(.*?\)", s)
print(t)
['hello(x, ...)', 'there(6L)', 'now()']
CodePudding user response:
You can split on a lookbehind for ()
and negative lookahead for the end of the string.
t = re.split(r'(?<=\(\))(?!$)', s)