Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
First solution in Clear category for Between Markers by vlad.bezden
"""
Between Markers.
You are given a string and two markers (the initial and final).
You have to find a substring enclosed between these two markers.
But there are a few important conditions:
The initial and final markers are always different.
If there is no initial marker then the beginning should be considered as the beginning of a string.
If there is no final marker then the ending should be considered as the ending of a string.
If the initial and final markers are missing then simply return the whole string.
If the final marker is standing in front of the initial one then return an empty string.
Input: Three arguments. All of them are strings. The second and third arguments are the initial and final markers.
Output: A string.
Precondition: can't be more than one final marker and can't be more than one initial
"""
def between_markers(text: str, begin: str, end: str) -> str:
"""
Returns substring between two given markers
"""
return text[
[0, text.find(begin) + len(begin)][begin in text]:
[None, text.find(end)][end in text]
]
if __name__ == "__main__":
assert between_markers("What is >apple<", ">", "<") == "apple", "One sym"
assert (
between_markers(
"My new site", "", ""
)
== "My new site"
), "HTML"
assert between_markers("No[/b] hi", "[b]", "[/b]") == "No", "No opened"
assert between_markers("No [b]hi", "[b]", "[/b]") == "hi", "No close"
assert between_markers("No hi", "[b]", "[/b]") == "No hi", "No markers at all"
assert between_markers("No ", ">", "<") == "", "Wrong direction"
Sept. 29, 2018