String segmentation with html tags in python
Question: I am trying to break a string into smaller segments using Python. The various
cases can be:  
str1 = "Hello world. This is an ideal example string."  
Result: Hello world.  
This is an ideal example string.

str2 = "`<H1>`Hello world.`</H1><P>`This is an HTML example string.`<P>`"  
Result:  
`<H1>`Hello world.`</H1>`  
`<P>`This is an HTML example string.`<P>`

str3 = "1. Hello World. 2. This is a string."  
Result:  
1\. Hello World.  
2\. This is a string.

Here is my code. But I cannot seem to achieve the 2nd case:

    
    
    import re
    
    string = """<h1>This is a string.</h1><a href="www.abc.com"> This is another part. <P/>"""
    
    segment_regex = re.compile(r"""
        (
            \r\n|
            \\r\\n|
            \n|
            \\n|
            \r|
            \\r|
            \t|
            \\t|
            (?:
                (?<=[^\d][\.|\!|\?])
                \s+
                (?=[A-Z0-9])
            )|
            (?:
                (?<=[\.|\!|\?])\s*(?=<.*?>)
            )
        )
        """, re.VERBOSE)
    seg = segment_regex.split(string)
    segments = seg[::2]
    separator = seg[1::2]
    
    print("Segments are ---->>")
    for s in segments:
        print (s)
    
    print("Separators are ---->>")
    for p in separator:
        print (p)
    


Answer: The regex may be trying to do too many things at once. A simpler and more
manageable way would be to first detect the string type html, ideal, list
first and then invoke appropriate processors for each. Something like :-

    
    
    import re
    
    string = """<h1>This is a string.</h1><a href="www.abc.com"> This is another part. <P/>"""
    if re.search('<.*?>', string):
        split_html(string)
    elif re.search('\\d\\.', string):
        split_list(string)
    else:
        split_ideal(string)
    

Also while this may work for the cases mentioned a generic "splitter" will be
far more complex and I don't claim that this approach will work for all.

