diff options
author | Kevin Gimbel <hallo@kevingimbel.com> | 2020-02-28 13:35:38 +0100 |
---|---|---|
committer | Kevin Gimbel <hallo@kevingimbel.com> | 2020-02-28 13:35:38 +0100 |
commit | d43175cb0dcf22633f76ef57bf56c47c4e6a2284 (patch) | |
tree | 15a08f302c01613d09aabe4c9f89457da01b95bc | |
parent | 40d3df4d2564b967e703615b1483c2cd05a30859 (diff) |
fix: fix replace error
The Regex was too greedy, resulting in the replacement of all content between openening and closing
comments when multiple comments were present. For now, only 1 (the first) match is replaced and the
regex is non-greedy so it matches the first end tag.
-rw-r--r-- | src/lib.rs | 5 |
1 files changed, 3 insertions, 2 deletions
@@ -120,9 +120,10 @@ pub fn make_toc( ) -> Result<String, ::std::io::Error> { let content = read_file(file_path_in)?; let new_toc = generate_toc(content.to_owned(), min_depth, max_depth); - let re_toc = regex::Regex::new(r"(?ms)^(<!-- BEGIN mktoc).*(END mktoc -->)").unwrap(); + let re_toc = + regex::Regex::new(r"(?ms)^(<!-- BEGIN mktoc -->)(.*?)(<!-- END mktoc -->)").unwrap(); let res: String = re_toc - .replace_all(content.as_str(), new_toc.as_str()) + .replacen(content.as_str(), 1, new_toc.as_str()) .into_owned(); Ok(res) |