【问题标题】:how to detect spaces, special characters in html tags in python如何检测python中html标签中的空格,特殊字符
【发布时间】:2016-11-26 00:15:15
【问题描述】:

对于以下输入

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

我想要如下所需的输出,如果存在&lt; 后跟空格,则应该会发生这种情况。

I/O 1<img>'<input>

谁能帮我处理正则表达式?

【问题讨论】:

    标签: python html regex


    【解决方案1】:
    s= "I/O 1<   img   >    '<    input   >"
    

    使用 s.find('

    s[0 : s.find('

    s[s.find('

    s.replace(' ','') 将用 no_spaces 替换空格

    ( s[0:s.find('<')] ) + ( s[s.find('<'):].replace(' ','') )
    

    【讨论】:

    • 嗨,空格,特殊字符可以是任意长度。感谢您的回复。主要目的是修剪空格,“之后的特殊字符
    • 您好,我刚刚进行了编辑。使用 s.find(' 从 '
    • edit解释这如何帮助解决用户的问题。
    【解决方案2】:

    试试&lt;\s+\s+&gt;&gt;\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)
    

    输出:

    I/O 1<img>'<input>
    

    【讨论】:

    • 我已经定义了我的 starttagopen = re.compile('a-zA-Z]'),我怎样才能修改成上面的代码
    • @Venu 我不明白。如果您已经尝试过代码,请编辑您的问题并将其包含在其中。
    猜你喜欢
    相关资源
    最近更新 更多
    热门标签