Nested Loop lines in a file n times (Example 3) below: Python
Question: I have a file with lines which need to be repeated as many times as the
decimal/hex value mentioned in the first line.

Input Example:

    
    
    Loop  3  {Line1}  ##the line is within curly braces so I used regex but not printing it out right. 
    Label: Blank {Line2}
    Jump Label {Line3} 
    

Output Example:

    
    
    Line1
    Line2
    Line3
    Line2
    Line3
    Line2
    Line3
    

My code so far:

    
    
    line_1_ = re.compile(r'Loop\s*([0-9]+|0x[0-9a-fA-F]+)\s*\{(\w+)\}', re.DOTALL)
    for match in line_1_.finditer(inputfileContents):
        looptimes =  int(match.group(1))
        line_1 = match.group(2)
        jump = re.search(r'Jump\s*(\w+)\s*\{(.*?)\}', inputfileContents, re.DOTALL)
        label = re.search(r'(\w+):\s*(\w+)\s*\{(\w+)\}', inputfileContents, re.DOTALL)
        for i in range(looptimes):
            if jump.group(1) == label.group(1):
                print '%s\n%s' % (label.group(3), jump.group(2))
    

Error: I cannot increment a line with line++. Which I understand as it is a
string but not sure how to increment it otherwise.

Effectively it is just repeating Line2 and Line 3 3 times. But if there are
multiple lines between the Jump and loop it has to print all the lines
starting from Label statement to the jump statement including the jump
statement.

This doesn't work if there are multiple lines between the jump and label
statements...

Example 2:

    
    
    Blank {Line0}
    Loop  3  {Line1}  
    Label: Blank {Line2}
           Blank {Line3}
           Blank {Line4}
    Jump Label {Line5}
    Blank {Line6}
    

Expected Output int he above case:

    
    
    Line0
    Line1
    Line2
    Line3
    Line4
    Line5
    Line2
    Line3
    Line4
    Line5
    Line2
    Line3
    Line4
    Line5
    Line6
    

Example 3:

    
    
    Blank  {Line0}
    Loop 3 {Line1}
    Label2 {Line2}
    Blank  {Line3}
    Loop 2 {Line4}  
    Label1:{Line5}
    Blank  {Line6}
    Jump Label1 {Line7}
    Blank  {Line8}
    Jump Label2 {Line9}
    Blank  {Line10}
    

Output I need:

    
    
    Line0
    Line1
    Line2
    Line3
    Line4
    Line5
    Line6
    Line7
    Line5
    Line6
    Line7
    Line8
    Line9
    Line2
    Line3
    Line4
    Line5
    Line6
    Line7
    Line5
    Line6
    Line7
    Line8
    Line9
    Line2
    Line3
    Line4
    Line5
    Line6
    Line7
    Line5
    Line6
    Line7
    Line8
    Line9
    Line10
    

I tried to implement @Samy Arous method from here: [Using Python to Parse a
File for Nested Loops](http://stackoverflow.com/questions/22327002/using-
python-to-parse-a-file-for-nested-loops) but couldn't implement it so far.


Answer: I found it easier without regex:

    
    
    import sys
    
    infile = sys.stdin
    
    def extract(line):
        return line[line.find('{') + 1: line.rfind('}')]
    
    for first in infile:
        if first.startswith('Loop '):
            break
        else:
            print extract(first)
    
    count = int(first.split()[1])
    lines = [extract(first)] + map(extract, infile)
    
    while count > 0:
        print '\n'.join(lines)
        count -= 1
    

