Ad

Using Regex To Split A String Recursively

- 1 answer

How do I go from this input to the following output?

word = "f(a(a,b), b(c,d))

def convert(word):    
    ...
    return fixed_word

print(convert(word))

>> ["f", "(a(a,b), b(c,d))"]

Additionally, how would I go about recursively creating the list? For example if I want the list to instead look like:

word = "f(a(a,b), b(c,d))

print(convert(word))

>> ["f", "a", "a", "b", "b", "c", "d"]

I've tried using regex for this but haven't managed to come up with a valid solution so any help would be much appreciated.

Ad

Answer

I'm not completely sure what you wish to achieve but for your first example i would recommend something like:

mo = re.match(re.compile(r"(\w)(\(.*\))"), word)
#                            f   (a(a,b)...)
result = list(mo.groups()) # or just mo.groups() if it can be a tuple instead of a list

For the second one I would refer you to the other answers you've gotten if you only want to split single characters. However if you need to have multiple characters you could split the string on any non-word character like:

word = "f(a(ab,bc), ba(c,d))"
result = re.split(re.compile(r"\W+"), word)
# ['f', 'a', 'ab', 'bc', 'ba', 'c', 'd', '']

If you're having troubles with regex it might help you to find a site like regex101 and of course to look in the documentation of the re module

Ad
source: stackoverflow.com
Ad