Some issues with Python regex findall
Question: Got string source :

    
    
    string ="""
    html,, 
        head,,        profile http://gmpg.org/xfn/11 ,,
                      lang en-US ,,
    
            title,,   Some markright page.
            ,,title
        ,,head
    """
    

...which have to parse as html :

    
    
    <html>
    <head profile="http://gmpg.org/xfn/11" lang="en-US">
    <title>Some markright page</title>
    </head>
    

I want to parse it with one `re.findall` pass like :

    
    
    tagList = re.findall( 
        r'\s*([A-Z]?[a-z]+[0-9]?,,){1}'   # Opening tag - has to be one
        r'(.* ,,)*'                       # Attributes - could be more than one
        r'(.*)?'                          # Content - could be one
        r'(\s+,,[a-z]+[0-9]?)?'           # Ending tag - could be one
        , string )#, flags=re.S )  # can't make any use of DOTALL flag   
    
    for t in tagList :
        n=0
        for s in t :
            n+=1
            print "String group No:"+str(n)+" -> ", s.strip()
        print "_"*10
    

...but got only:

    
    
    String group No:1 ->  html,,
    String group No:2 ->  
    String group No:3 ->  
    String group No:4 ->  
    __________
    String group No:1 ->  head,,
    String group No:2 ->  profile http://gmpg.org/xfn/11 ,,
    String group No:3 ->  
    String group No:4 ->  
    __________
    String group No:1 ->  title,,
    String group No:2 ->  
    String group No:3 ->  Some markright page.
    String group No:4 ->  ,,title
    

Please, keep in mind that I **have** to make my own parser, and upper
mentioned problem is just one appliance of this markup superset so help if you
can & want. Thanks.


Answer: This is the way I will do it:

    
    
    #!/usr/bin/python
    import re
    
    pat = re.compile(r'''
        (?P<open> \b [^\W_]+ ) ,, |
        ,, (?P<close> [^\W_]+ ) \b |
        (?P<attrName> \S+ ) [ ] (?P<attrValue> [^,\n]+ ) [ ] ,, |
        (?P<textContent> [^,\s] (?: [^,] | , (?!,) )*? ) \s* (?=[^\W_]*,,)''',
          re.X)
    
    txt = '''html,, 
        head,,        profile http://gmpg.org/xfn/11 ,,
                      lang en-US ,,
    
            title,,   Some markright page.
            ,,title
        ,,head'''
    
    result = ''
    opened = False
    for m in pat.finditer(txt):
        if m.group('attrName'):
            result += ' ' + m.group('attrName') + '="' + m.group('attrValue') + '"'
        else:
            if opened:
                opened = False
                result += '>'
            if m.group('open'):
                result += '<' + m.group('open')
                opened = True
            elif m.group('close'):
                result += '</' + m.group('close') + '>'
            else:
                result += m.group('textContent')
    print result
    

Note: I assume that the text content is always enclosed between tags.

