3

I have difficulties splitting a string into specific parts in Python 3. The string is basically a list with a colon (:) as a delimiter.

Only when the colon (:) is prefixed with a backslash (\), it does not count as a delimiter but part of the list item.

Example:

String --> I:would:like:to:find\:out:how:this\:works

Converted List --> ['I', 'would', 'like', 'to', 'find\:out', 'how', 'this\:works']

Any idea how this could work?

@Bertrand I was trying to give you some code and I was able to figure out a workaround but this is probably not the most beautiful solution

text = "I:would:like:to:find\:out:how:this\:works"

values = text.split(":")
new = []
concat = False
temp = None

for element in values:

    # when one element ends with \\

    if element.endswith("\\"):
        temp = element
        concat = True

    # when the following element ends with  \\ 
    # concatenate both before appending them to new list

    elif element.endswith("\\") and temp is not None:
        temp = temp + ":" + element
        concat = True

   # when the following element does not end with \\
   # append and set concat to False and temp to None

    elif concat is True:
        new.append(temp + ":" + element)
        concat = False
        temp = None

   # Append element to new list

    else:
        new.append(element)

print(new)

Output:

['I', 'would', 'like', 'to', 'find\\:out', 'how', 'this\\:works']
Melvin
  • 407
  • 5
  • 16
  • hi, maybe you can attach your code – Bertrand Jun 22 '18 at 15:25
  • @Bertrand I have edited the post – Melvin Jun 22 '18 at 15:47
  • What is your final expected o/p? In this case, what should be the value of **new**? – hygull Jun 22 '18 at 15:49
  • @RishikeshAgrawani At first I have split the string by colon and reconcatenated elements that and with (\\) with the following element. It then looks like the example of the converted list above. – Melvin Jun 22 '18 at 15:53
  • 1
    Thx! @MelvinTomKlimke it's always better to attach your code sample to understand your problem. I guess the answers below solve your problem :) – Bertrand Jun 22 '18 at 15:54
  • `Converted List --> ['I', 'would', 'like', 'to', 'find\:out', 'how', 'this\:works']` is what you expect, correct? – hygull Jun 22 '18 at 15:55
  • @RishikeshAgrawani Exactly! To answer Bertrand's request to give him some code, I was suddenly able to get this workaround done which actually works now but is not beautiful. I guess my question was then answered below. – Melvin Jun 22 '18 at 15:57

3 Answers3

5

You should use re.split and perform a negative lookbehind to check for the backslash character.

import re
pattern = r'(?<!\\):'
s = 'I:would:like:to:find\:out:how:this\:works'
print(re.split(pattern, s))

Output:

['I', 'would', 'like', 'to', 'find\\:out', 'how', 'this\\:works']
Jim Wright
  • 5,905
  • 1
  • 15
  • 34
2

You can replace the ":\" with something (just make sure that this is something that doesn`t exist in the string in other place... you can use a long term or something), and than split by ":" and replace it back.

[x.replace("$","\:") for x in str1.replace("\:","$").split(":")]

Explanation:

str1 = 'I:would:like:to:find\:out:how:this\:works'

Replace ":" with "$" (or something else):

str1.replace("\:","$")
Out: 'I:would:like:to:find$out:how:this$works'

Now split by ":"

str1.replace("\:","$").split(":")
Out: ['I', 'would', 'like', 'to', 'find$out', 'how', 'this$works']

and replace "$" with ":" for every element:

[x.replace("$","\:") for x in str1.replace("\:","$").split(":")]
Out: ['I', 'would', 'like', 'to', 'find\\:out', 'how', 'this\\:works']
theletz
  • 1,713
  • 2
  • 16
  • 22
1

Use re.split

Ex:

import re
s = "I:would:like:to:find\:out:how:this\:works"
print( re.split(r"(?<=\w):", s) )

Output:

['I', 'would', 'like', 'to', 'find\\:out', 'how', 'this\\:works']
Rakesh
  • 81,458
  • 17
  • 76
  • 113