regex - Parse string in python -
i turn this:
mystr = ' foo1 (foo2 foo3 (foo4))'
into:
['foo1','foo2 foo3 (foo4)']
so have split based on number of spaces/tabs , parenthesis.
i have seen re package split function can handle several delimiters (python: split string multiple delimiters) can not manage understand right approach parse kind of strings.
which best -most pythonic- , simple approach?
as far can understand, consistent want, , pretty simple. uses slicing isolate first word , part between parentheses. has use strip
couple of times due spaces. may seem little verbose, honest if task can accomplished such simple string operations feel complicated parsing unnecessary (although may have gotten wrong). note is flexible in amount of whitespace split by.
mystr = ' foo1 (foo2 foo3 (foo4))' mystr = mystr.strip() = mystr.index(' ') = mystr[:i].strip() b = mystr[i:].strip()[1:-1] print([a, b])
with output
['foo1', 'foo2 foo3 (foo4)']
although i'm still not entirely clear if want. let me know if works or needs changing.
Comments
Post a Comment