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

2024-05-04 01:34:13 发布

您现在位置:Python中文网/ 问答频道 /正文

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)

但这给了我一个值错误,因为它不能得到任何结果 有什么办法吗?你知道吗


Tags: stringindexlenreturnthatdeflinebob
2条回答

使用re.search

>>> re.search(r' (.*)', String).group(1)
' things'

使用字符串方法:

for string in text.splitlines():
    if '   ' in string:
        print(string.strip().split('   ', 1)[1])

相关问题 更多 >