0

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!

3
  • What exactly is the input? Whats wrong with foo.find('/The quick brown fox/i')? Commented May 26, 2014 at 22:28
  • the input is the one big string. I want the line that is 2 lines above a line that starts with "0:" Commented May 26, 2014 at 22:35
  • 1
    Read all lines into list lines, find on list line with '0:' and then lines[index-2] Commented May 26, 2014 at 22:42

1 Answer 1

2

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:'.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.