How to extract the substring between two markers?

前端 未结 18 2363
慢半拍i
慢半拍i 2020-11-22 06:02

Let\'s say I have a string \'gfgfdAAA1234ZZZuijjk\' and I want to extract just the \'1234\' part.

I only know what will be the few characte

18条回答
  •  太阳男子
    2020-11-22 06:25

    Here's a solution without regex that also accounts for scenarios where the first substring contains the second substring. This function will only find a substring if the second marker is after the first marker.

    def find_substring(string, start, end):
        len_until_end_of_first_match = string.find(start) + len(start)
        after_start = string[len_until_end_of_first_match:]
        return string[string.find(start) + len(start):len_until_end_of_first_match + after_start.find(end)]
    

提交回复
热议问题