-
Notifications
You must be signed in to change notification settings - Fork 0
/
Between markers.py
32 lines (28 loc) · 1.34 KB
/
Between markers.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
def between_markers(text: str, begin: str, end: str) -> str:
"""
returns substring between two given markers
"""
marker_pos_start = text.find(begin)
marker_pos_end = text.find(end)
if marker_pos_start == -1 and marker_pos_end == -1:
return text
elif marker_pos_start == -1:
return text[:marker_pos_end]
elif marker_pos_end == -1:
return text[marker_pos_start+len(begin):]
elif marker_pos_end < marker_pos_start:
return ''
else:
return text[marker_pos_start+len(begin):marker_pos_end]
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("<head><title>My new site</title></head>",
"<title>", "</title>") == "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 <hi>', '>', '<') == '', 'Wrong direction'
print('Wow, you are doing pretty good. Time to check it!')