realp
realp

Reputation: 627

Parsing python output

Hey guys how can i parse this output in Python:

 /abc{3,}zz/
         the quick brown fox
 No match

 /The quick brown fox/i
         the quick brown fox
  0: the quick brown fox

 /a*abc?xyz+pqr{3}ab{2,}xy{4,5}pq{0,6}AB{0,}zz/
         the quick brown fox
 No match

to get this string "/The quick brown fox/i"

I considered using something like foo.find("0:") but from there I can't think of how to get the line above this should be pretty simple thanks for the help!

Upvotes: 0

Views: 90

Answers (1)

timgeb
timgeb

Reputation: 78800

Answering based on your comment:

the input is the one big string. I want the line that is 2 lines above a line that starts with "0:"

The approach is: 1. Create a list holding each line, 2. Find lines which start with 0:, not counting leading whitespace (strip will remove trailing and leading whitspace), 3. Print the number of the line two lines above any match

inputstr="""/abc{3,}zz/
        the quick brown fox
No match

/The quick brown fox/i
        the quick brown fox
 0: the quick brown fox

/a*abc?xyz+pqr{3}ab{2,}xy{4,5}pq{0,6}AB{0,}zz/
        the quick brown fox
No match"""

inputlst=inputstr.split('\n')
for i in range(2,len(inputlst)):
    if inputlst[i].strip().startswith('0:'):
        print(i - 1)

This will print 5. It also assumes Line 1 and 2 don't start with '0:'.

Upvotes: 2

Related Questions