Python Regex Mark-Up
Question: Hi guys having trouble with a particular problem. I am using python's regex to
alter the markup source to output html format.

markup source:

    
    
    [ 
    # sometextsometextsometextsometextsometextsometext.  #
    
    # sometextsometextsometextsometextsometextsometextsometextsometext
    sometextsometextsometextsometextsometextsometext. #
    ]
    
    
    [
    hello i am a normal paragraph.
    ]
    

desired output:

    
    
    <ol> 
    <li> sometextsometextsometextsometextsometextsometext.  </li>
    
    <li> sometextsometextsometextsometextsometextsometextsometextsometext
    sometextsometextsometextsometextsometextsometext. </li>
    </ol>
    
    <p>
    hello i am a normal paragraph.
    </p>
    


Answer: 
    import re
    with open('mk.txt') as f:
        with open('newmk.txt','w+') as g:
            text = f.read()
            SquareGroups = re.findall(r'\[(?:.|\n)+?\]',text)
            for group in SquareGroups:
                if '#' in group: #must be ol
                    group = group.replace('[','<ol>')
                    group = group.replace(']','</ol>')
                    group = re.sub('#(?= ?\w)','<li>',group)
                    group = re.sub('(?<=[\w ])#','</li>',group)
                else:
                    group = group.replace('[','<p>')
                    group = group.replace(']','</p>')
                g.write(group)
                g.write('\n') #optional, just makes the output look 'nicer'
    

Transforms your input in `mk.txt` into the following text in `newmk.txt`:

    
    
    <ol>
    <li> sometextsometextsometextsometextsometextsometext.  </li>
    
    <li> sometextsometextsometextsometextsometextsometextsometextsometext
    sometextsometextsometextsometextsometextsometext. </li>
    </ol>
    <p>
    hello i am a normal paragraph.
    </p>
    

