开发者

Python Function To Find String Between Two Markers [duplicate]

This question already has answers here: How to extract the substring between two markers? (22 answers) Closed 4 years ago.

I'm looking to build a string function to extract the string contents between two markers. It returns an extraction list

def extract(raw_string, start_marker, end_marker):
    ... function ...
    return extraction_list

I know this ca开发者_StackOverflown be done using regex but is this fast? This will be called billions of times in my process. What is the fastest way to do this?

What happens if the markers are the same and appear and odd number of times?

The function should return multiple strings if the start and end markers appear more than once.


You probably can't go faster than:

def extract(raw_string, start_marker, end_marker):
    start = raw_string.index(start_marker) + len(start_marker)
    end = raw_string.index(end_marker, start)
    return raw_string[start:end]

But if you want to try regex, just try to benchmark it. There's a good timeit module for it.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜