python-checkio/Elementary/between-markers-simplified.py

20 lines
715 B
Python
Raw Normal View History

2019-12-24 14:52:17 +01:00
import re
def between_markers(text: str, begin: str, end: str) -> str:
"""
returns substring between two given markers
"""
return re.findall(r"{}(.*){}".format(re.escape(begin), re.escape(end)), text)[0]
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"
assert between_markers('What is [apple]', '[', ']') == "apple"
assert between_markers('What is ><', '>', '<') == ""
assert between_markers('>apple<', '>', '<') == "apple"
print('Wow, you are doing pretty good. Time to check it!')