使用String.replace替换Rust中字符串的多行部分

使用String.replace替换Rust中字符串的多行部分,rust,Rust,我正在尝试使用Rust对HTML文件进行检查和更改。基本上,我在代码中找到需要替换的特定内容,然后使用String.replace(stringToReplace,replacingString)将其替换为其他内容。在我尝试替换它的多行部分之前,它一直运行良好。例如: index.html <div id="outer"> <h1>Some title</h1> <p>Some text</p> &l

我正在尝试使用Rust对HTML文件进行检查和更改。基本上,我在代码中找到需要替换的特定内容,然后使用String.replace(stringToReplace,replacingString)将其替换为其他内容。在我尝试替换它的多行部分之前,它一直运行良好。例如:

index.html

<div id="outer">
    <h1>Some title</h1>
    <p>Some text</p>
</div>

一些头衔
一些文本

newIndex.html

<p>I have replaced the whole div</p>
我已经替换了整个div

无论我尝试了什么,我都无法使字符串与多行匹配。它只是永远不会取代它。我如何在代码的多行部分进行匹配。我曾想过每一行都单独完成,但显然我做不到,因为替换
并不好

下面是我正在使用的代码。我无法对原始代码进行任何更改以使其更易于匹配。在main.rs中,我循环执行Change.changes。偶数索引是要替换的字符串,奇数索引是替换的文本

梅因

use std::fs::File;
use std::io::prelude::*;

mod ejs;
mod js;
mod classes;
mod controllers;
mod change;

fn main() {
    let ejs_files: Vec<change::Change> = ejs::change();
    let js_files: Vec<change::Change> = js::change();
    let class_files: Vec<change::Change> = classes::change();
    let controller_files: Vec<change::Change> = controllers::change();

    for i in 0..ejs_files.len() {
        let mut contents = match read_file(&ejs_files[i].location) {
            Ok(contents) => contents,
            Err(e) => panic!(e)
        };

        let mut j = 0;
        while j < ejs_files[i].changes.len() {
            contents = contents.replace(ejs_files[i].changes[j], ejs_files[i].changes[j+1]);
            j += 2;
        }

        match write_file(&ejs_files[i].location, contents) {
            Err(e) => panic!(e),
            _ => ()
        };
    }

    for i in 0..js_files.len() {
        let mut contents = match read_file(&js_files[i].location) {
            Ok(contents) => contents,
            Err(e) => panic!(e)
        };

        let mut j = 0;
        while j < js_files[i].changes.len() {
            contents = contents.replace(js_files[i].changes[j], js_files[i].changes[j+1]);
            j += 2;
        }

        match write_file(&js_files[i].location, contents) {
            Err(e) => panic!(e),
            _ => ()
        };
    }

    for i in 0..class_files.len() {
        let mut contents = match read_file(&class_files[i].location) {
            Ok(contents) => contents,
            Err(e) => panic!(e)
        };

        let mut j = 0;
        while j < class_files[i].changes.len() {
            contents = contents.replace(class_files[i].changes[j], class_files[i].changes[j+1]);
            j +=2;
        }

        match write_file(&class_files[i].location, contents) {
            Err(e) => panic!(e),
            _ => ()
        };
    }

    for i in 0..controller_files.len() {
        let mut contents = match read_file(&controller_files[i].location) {
            Ok(contents) => contents,
            Err(e) => panic!(e)
        };

        let mut j = 0;
        while j < controller_files[i].changes.len() {
            contents = contents.replace(controller_files[i].changes[j], controller_files[i].changes[j+1]);
            j +=2;
        }

        match write_file(&controller_files[i].location, contents) {
            Err(e) => panic!(e),
            _ => ()
        };
    }
}

fn read_file(file: &str) -> std::io::Result<String> {
    let mut file = File::open(file)?;
    let mut contents = String::new();
    file.read_to_string(&mut contents)?;
    Ok(contents)
}

fn write_file(file: &str, contents: String) -> std::io::Result<()> {
    let mut file = File::create(file)?;
    file.write(contents.as_bytes())?;
    Ok(())
}
使用std::fs::File;
使用std::io::prelude::*;
mod ejs;
mod js;
mod类;
mod控制器;
模态变化;
fn main(){
让ejs_文件:Vec=ejs::change();
让js_文件:Vec=js::change();
让class_文件:Vec=classes::change();
让控制器_文件:Vec=controllers::change();
对于0..ejs_files.len()中的i{
让mut contents=match read_文件(&ejs_文件[i]。位置){
Ok(目录)=>目录,
错误(e)=>恐慌!(e)
};
设mut j=0;
而j恐慌!(e),
_ => ()
};
}
对于0..js_files.len()中的i{
让mut contents=match read_文件(&js_文件[i]。位置){
Ok(目录)=>目录,
错误(e)=>恐慌!(e)
};
设mut j=0;
而j恐慌!(e),
_ => ()
};
}
对于0..class_files.len()中的i{
让mut contents=匹配读取文件(&class\u文件[i]。位置){
Ok(目录)=>目录,
错误(e)=>恐慌!(e)
};
设mut j=0;
而j恐慌!(e),
_ => ()
};
}
对于0..controller_files.len()中的i{
让mut contents=匹配读取文件(&控制器文件[i]。位置){
Ok(目录)=>目录,
错误(e)=>恐慌!(e)
};
设mut j=0;
而j恐慌!(e),
_ => ()
};
}
}
fn read_文件(文件:&str)->std::io::Result{
让mut file=file::open(file)?;
让mut contents=String::new();
读取到字符串(&mut contents)?;
Ok(目录)
}
fn write_文件(文件:&str,内容:String)->std::io::Result{
让mut file=file::create(file)?;
file.write(contents.as_bytes())?;
好(())
}
零钱

pub struct Change<'a> {
    pub location: String,
    pub changes: Vec<&'a str>
}
pub结构更改
}
js.rs(简短示例):

使用板条箱::更改::更改;
pub fn change>{
let folder_location:&str=“../javascript/InventoryManagement/”;
让家改变{
位置:format!(“{}{}”,文件夹位置,“views/dashboardPage/js/streams/home.js”),
改变:vec[
“与上月相比,”,
“收入”、“Бааа”,
“\”日期“,“\”БАСАА\”,
“最受欢迎的配料”,“САМ峈П峈ППППУЛЯ峈峈峈峈峈峈峈23752,
“数量”、“数量”
]
};
在main.rs中,我将浏览不同的文件,并替换某些东西。JS、HTML(EJS)以及最终的CSS。在我不得不进行多行更改之前,这些更改都很有效。如果我这样做,那么它就不起作用了

js.rs(多行):

使用板条箱::更改::更改;
pub fn change>{
let folder_location:&str=“../javascript/InventoryManagement/”;
让家改变{
位置:format!(“{}{}”,文件夹位置,“views/dashboardPage/js/streams/home.js”),
改变:vec[
"
我的多行评论

", “其他的东西

” ] }; vec![主页] }

如果有更好的方法,那么我也想知道。或者只是一种查找和替换代码多行部分的方法。我对Rust很陌生,这是我用它做的第一个项目。

是否需要添加字符?我也尝试过,但没有用。我在每行末尾添加了\n,然后运气好。空格也算在多行字符串中。这些匹配吗?数据中的缩进是否与要替换的完全相同?这种方法看起来很脆弱。你考虑过使用HTML5之类的HTML解析器吗?@TimothyAlexisVass我没有检查过。我会检查并尝试一下,看看这是否解决了我的问题.
use crate::change::Change;

pub fn change<'a>() -> Vec<Change<'a>> {
    let folder_location: &str = "../../javascript/InventoryManagement/";

    let home_js = Change {
        location: format!("{}{}", folder_location, "views/dashboardPage/js/strands/home.js"),
        changes: vec![
            "% vs last month", "% по сравнению с прошлым месяцем",
            "REVENUE", "ДОХОД",
            "\"DATE\"", "\"ДАТА\"",
            "MOST POPULAR INGREDIENTS", "САМЫЕ ПОПУЛЯРНЫЕ ИНГРЕДИЕНТЫ",
            "QUANTITY", "КОЛИЧЕСТВО"
        ]
    };
    
use crate::change::Change;

pub fn change<'a>() -> Vec<Change<'a>> {
    let folder_location: &str = "../../javascript/InventoryManagement/";

    let home_js = Change {
        location: format!("{}{}", folder_location, "views/dashboardPage/js/strands/home.js"),
        changes: vec![
            "<div id='something'>
                <p>My multi-line comment</p>
           </div>", 
           "<p>Something else</p>"
        ]
    };
    vec![home_js]
}