【问题标题】:Regular Expression removing <![CDATA[正则表达式删除 <![CDATA[
【发布时间】:2016-10-09 00:59:58
【问题描述】:

我有这个正则表达式:

</title>[\s]*<description[^>]*>(.*?)<img

接受字符串:

<title>Insane price of last Ford Falcon V8s</title>
        <description><![CDATA[FORD dealers are charging a staggering $30,000 more than the recommended retail price — up from $60,000 to $90,000 — for the final Falcon V8 sedans as buyers try to secure a future classic.<img alt="" border="0" src="https://pixel.wp.com/b.gif?host=www.couriermail.com.au&#038;blog=87782261&#038;post=1205849&#038;subd=couriermailatnewscorpau&#038;ref=&#038;feed=1" width="1" height="1" />]]></description>

如何编辑此正则表达式以同时删除 &lt;![CDATA[

【问题讨论】:

    标签: python html regex cdata


    【解决方案1】:

    正则表达式是非常强大的工具。这包括将错误带入代码的高风险,尤其是当您不知道如何准确处理它们时(这里似乎就是这种情况)。

    您应该始终首先使用 Python 的内置字符串类,并且在必要时使用 RegEx。

    如果您有字符串my_str,那么下面的代码将替换my_str 中的子字符串:

    my_str = "hello world"
    my_str.replace("lo", "")
    >>> "hel world"
    

    str.replace 在这种情况下搜索“lo”并将其替换为“”(什么都没有,因此将其删除)。当然,您可以随意更改此值。

    看看Python's documention for Strings

    【讨论】:

      猜你喜欢
      • 2023-04-03
      • 1970-01-01
      • 2019-02-16
      • 1970-01-01
      • 2019-11-08
      • 2011-03-05
      • 2011-06-04
      • 2019-08-20
      • 1970-01-01
      相关资源
      最近更新 更多