Leviathan GD
Leviathan GD

Reputation: 13

Find text after substring until the end of the line in python

String = """bob
123 -- things
stuff after that line"""

I need to get " things". I have tried

 def InBetween(Substring1, Substring2, String):
    return String[(String.index(Substring1)+len(Substring1)):String.index(Substring2)]
Stuff = InBetween("--", "\n", String)

But this gives me a ValueError due to the fact that it can not get any results any way to do this?

Upvotes: 1

Views: 1316

Answers (2)

Valery Ramusik
Valery Ramusik

Reputation: 1573

Using string methods:

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

Upvotes: 1

Sunitha
Sunitha

Reputation: 12015

Use re.search

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

Upvotes: 1

Related Questions