String = """bob
123 -- things
stuff after that line"""
我需要得到“东西”。 我尝试过
def InBetween(Substring1, Substring2, String):
return String[(String.index(Substring1)+len(Substring1)):String.index(Substring2)]
Stuff = InBetween("--", "\n", String)
但是这给了我一个ValueError,因为它无法得到任何结果 有什么办法吗?
答案 0 :(得分:0)
使用re.search
>>> re.search(r'--(.*)', String).group(1)
' things'
答案 1 :(得分:0)
使用字符串方法:
for string in text.splitlines():
if ' -- ' in string:
print(string.strip().split(' -- ', 1)[1])