def remove_select_tags(string, start_tag, end_tag):
start = string.find(start_tag)
while start != -1:
end = string.find(end_tag, start)
string = string[:start] + " " + string[end + 1:]
start = string.find(start_tag)
return string.split()
def test_case():
target_string = '''<h1>Title</h1><p>This is a
<a href="mt-takao.top">link</a>.<p>'''
assert remove_select_tags(target_string, '<', '>') == ['Title', 'This', 'is', 'a', 'link', '.']
target_string = "[test]a-I-U-E-O[test][next]Kakikukeko[next]"
assert remove_select_tags(target_string, '[', ']') == ['a-I-U-E-O', 'Kakikukeko']
print('test ok')
test_case()
Deletes the element of the specified character in the character string and retrieves the element. If the element is not found, -1 is returned, otherwise it is executed. Connect the part before the index found by start with a blank and connect it with the character string after the index found by end. Then look for more start_tags in the newly generated string. Finally, separate the spaces with commas to finish.
Recommended Posts