Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
Find and slice solution in Clear category for Between Markers by brubru777
def between_markers(text: str, begin: str, end: str) -> str:
"""
returns substring between two given markers
"""
lo = text.find(begin)
hi = text.rfind(end)
lo = 0 if lo == -1 else lo + len(begin)
hi = len(text) if hi == -1 else hi
return text[lo:hi] if lo < hi else ''
if __name__ == '__main__':
print('Example:')
print(between_markers('What is >apple<', '>', '<'))
# These "asserts" are used for self-checking and not for testing
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'
print('Wow, you are doing pretty good. Time to check it!')
Dec. 29, 2020