1

Ok so I know I can split a string using the .split method every time a character/a sequence of characters appears - for example, in the following code, when I use the .split method, the <string> is splitted every time : appears in it

<string>.split(":")

But I can't give it multiple arguments, so that the string is splitted if one of the arguments appears.

For example, given this input string:

input_string = """ \ hel\lo ="world"""

I want to split the input_string every time \, =, ", or <blank space> appears, so that i get the following output:

['hel', 'lo', 'world']

I know that a solution for this could be:

non_alfanumeric = """\\=" """
input_string = """ \ hel\lo ="world"""

for char in non_alfanumeric:
    input_string = input_string.replace(char, " ")

list = input_string.split(" ")
while "" in list:
    list.remove("")

but this would be too much computationally slow if the input string is very long, because the code has to check the whole string 4 times: first time for \, second time for =, third time for ", fourth time for <blank space>.

What are other solutions?

2 Answers2

2

You might want to look at regex splitting, something like

import re

[x for x in re.split(r'[= \\"]+', """ \ hel\lo ="world""") if x]
0

it seems that re.split will be good for your case.

marksman123
  • 324
  • 2
  • 11