From e29d0f2c00587250ec90eeb036cf4eb1ccb426f4 Mon Sep 17 00:00:00 2001 From: Hykilpikonna Date: Sat, 13 Aug 2022 17:50:02 -0400 Subject: [PATCH] [+] substr_between function --- hypy_utils/nlp_utils.py | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/hypy_utils/nlp_utils.py b/hypy_utils/nlp_utils.py index 2124a84..968ae92 100644 --- a/hypy_utils/nlp_utils.py +++ b/hypy_utils/nlp_utils.py @@ -29,3 +29,20 @@ def camel_split(camel: str) -> list[str]: # for "lUl", index of "U" will pop twice, have to filter that return [camel[x:y] for x, y in zip(word, word[1:]) if x < y] + + +def substr_between(s: str, start: str | None = None, end: str | None = None): + """ + Get substring between two strings + + :param s: E.g. "Foo abc Bar" + :param start: E.g. "Foo" + :param end: E.g. "Bar" + :return: E.g. " abc " + """ + if start: + s = s[s.index(start) + len(start):] + if end: + s = s[:s.index(end)] + return s +