Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/320.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 正则表达式删除_Python_Html_Regex_Cdata - Fatal编程技术网

Python 正则表达式删除

Python 正则表达式删除,python,html,regex,cdata,Python,Html,Regex,Cdata,我有一个正则表达式: </title>[\s]*<description[^>]*>(.*?)<img 其中包含以下字符串: 如何编辑此正则表达式以同时删除正则表达式是一个非常强大的工具。这包括将错误引入代码的高风险,特别是当您不知道如何准确处理它们时,这里似乎就是这种情况 您应该始终首先使用Python的内置字符串类,并且仅在必要时使用正则表达式 如果您有字符串my_str,则以下代码将替换my_str中的子字符串: str.replace在这种情况下搜索

我有一个正则表达式:

</title>[\s]*<description[^>]*>(.*?)<img
其中包含以下字符串:


如何编辑此正则表达式以同时删除

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

您应该始终首先使用Python的内置字符串类,并且仅在必要时使用正则表达式

如果您有字符串my_str,则以下代码将替换my_str中的子字符串:

str.replace在这种情况下搜索lo并将其替换为nothing,从而删除它。当然,您可以根据需要更改此值

看一看

<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>
my_str = "hello world"
my_str.replace("lo", "")
>>> "hel world"