Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/363.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_Leaflet - Fatal编程技术网

Python 使用正则表达式替换多次出现的字符串

Python 使用正则表达式替换多次出现的字符串,python,html,regex,leaflet,Python,Html,Regex,Leaflet,我使用Python编辑一个html文件,该文件用于映射传单。html文件包含用于功能的函数和用于样式的另一个相关函数。例如,我有一个风和气的特征函数和样式函数: function pop_Wind_1(feature, layer) { layer.on({ mouseout: function(e) { for (i in e.target._eventParents) { e.target._eventParen

我使用Python编辑一个html文件,该文件用于映射传单。html文件包含用于功能的函数和用于样式的另一个相关函数。例如,我有一个风和气的特征函数和样式函数:

function pop_Wind_1(feature, layer) {
    layer.on({
        mouseout: function(e) {
            for (i in e.target._eventParents) {
                e.target._eventParents[i].resetStyle(e.target);
            }
        },
        mouseover: highlightFeature,
    })};

function style_Wind_1_0() {
    return {
        fillColor: 'rgba(231,113,0,1.0)',
    }
}

function pop_Gas_2(feature, layer) {
    layer.on({
        mouseout: function(e) {
            for (i in e.target._eventParents) {
                e.target._eventParents[i].resetStyle(e.target);
            }
        },
        mouseover: highlightFeature,
    })};

function style_Gas_2_0() {
    return {
        fillColor: 'rgba(231,113,0,1.0)',
    }
}

对于行
e.target.\u eventParents[i].resetStyle(e.target)在第一个函数中,我要替换

resetStyle(e.target); 

因此,该行读取
e.target.\u eventParents[i].setStyle(style\u Wind\u 1\u 0(feature))

对于gas,它将是
e.target.\u eventParents[i].setStyle(style\u gas\u 2\u 0(feature))

如何使用正则表达式替换html文件中的所有事件?我尝试过regex
函数pop[^;]+(?=\b)
,但不确定如何遍历每个函数并用正确的样式替换它

到目前为止,我的Python代码:

path = path/to/file
with open(index.path, "r+") as f:
    contents = f.read()
    f.seek(0)
    new_contents = re.sub(r'function pop[^;]+(?=\b)', r'', contents)

f.write(new_contents)
f.truncate()
这里确实需要一个解析器来识别JavaScript函数。
path = path/to/file
with open(index.path, "r+") as f:
    contents = f.read()
    f.seek(0)
    new_contents = re.sub(r'function pop[^;]+(?=\b)', r'', contents)

f.write(new_contents)
f.truncate()