在子字符串后查找文本,直到python中的行末

时间:2018-08-15 22:37:07

标签: python string

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,因为它无法得到任何结果 有什么办法吗?

2 个答案:

答案 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])