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?

selenio34
  • 92
  • 1
  • 9
  • 1
    `re.split('[\\\\=" ]', text)` ? – furas May 23 '20 at 18:12
  • @furas perfect! thank you very much. One question: why did you put the square brackets before the characters in '[\\\\="]'? – selenio34 May 23 '20 at 18:16
  • https://trinket.io/python3/43e0da34c2 – Pedro Lobito May 23 '20 at 18:16
  • 1
    you have to remove empty strings - you can use `filter` with `None` - `list(filter(None, re.split('[\\\\=" ]', text)))` – furas May 23 '20 at 18:18
  • @furas yeah ok this works even better, thanks. I'm new to StackOverflow, is there a way I can give you points by upvoting your comment or something? – selenio34 May 23 '20 at 18:20
  • regex use `[ ]` to create list of chars - without `[ ]` it will treat it as single word – furas May 23 '20 at 18:20
  • question is closed so I can't add answer - you can only upvote my comments (if you only will have enough reputation/points to do this). – furas May 23 '20 at 18:22

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
  • 389
  • 2
  • 11