[SOLVED] Separate string by multiple separators and return separators and separated strings

Issue

I want to separate strings by separators constist of more than one char saved in the variable sep_list.

My aim then is to receive the last separated string s1 and the last separator which has s1 on his right hand side.

sep_list = ['→E', '¬E', '↓I']

string1 = "peter →E tom ¬E luis ↓I ed"
string2 = "sigrid →E jose l. ¬E jose t."

Applied on string1 the algorithm should return the string s1:

"↓I, ed"

and applied on string2 the algorithm should return the string s1:

"¬E, jose t."

What is a way to do that with python?

Solution

Another way to do so using regex:

import re

sep_list = ['→E', '¬E', '↓I']

string1 = "peter →E tom ¬E luis ↓I ed"
string2 = "sigrid →E jose l. ¬E jose t."

def separate_string(data, seps):
    pattern = "|".join(re.escape(sep) for sep in seps)
    start, end = [m.span() for m in re.finditer(pattern, data)][-1]

    return f"{data[start:end]},{data[end:]}"

print(separate_string(string1, sep_list))  # ↓I, ed
print(separate_string(string2, sep_list))  # ¬E, jose t.

  • We create a regex pattern by separating each keyword with |.
  • For each match in the string, we use m.span() to retrieve the start and end of the match. We only keep the last match.
  • data[start:end] is the separator, while data[end:] is everything after.

Answered By – Cubix48

Answer Checked By – David Goodson (BugsFixing Volunteer)

Leave a Reply

Your email address will not be published. Required fields are marked *