how to detect spaces, special characters in html tags in python
Question: For the following input

    
    
    I/O 1<   img   >    '<    input   >
    I/O 1<'   img   >    '<    input   >
    

I want the required output as below and this should happen if `<` followed by
space is present.

    
    
    I/O 1<img>'<input>
    

Can anyone help me with regular expression?


Answer: Try `<\s+`, `\s+>`, and `>\s+`:

    
    
    import re
    
    s = "I/O 1<   img   >    '<    input   >"
    s = re.sub(r"<\s+", "<", s)
    s = re.sub(r"\s+>", ">", s)
    s = re.sub(r">\s+", ">", s)
    print(s)
    

Output:

    
    
    I/O 1<img>'<input>
    

