// include the latest version of the regex crate in your Cargo.toml
extern crate regex;
use regex::Regex;
fn main() {
let regex = Regex::new(r"(?x)(?|
\G(?!\A) # contiguous to the precedent match (inside a comment)
(?|
-{2,}+([^->][^-]*) # duplicate hyphens, not part of the closing sequence
|
(-[^-]+) # preserve isolated hyphens
|
-+ (?=-->) # hyphens before closing sequence, break the contiguity
|
-->[^<]* (*SKIP)(*FAIL) # closing sequence, go to next <, break contiguity
)
|
[^<]*<+ # reach the next < (outside comment)
(?> [^<]+ <+ )*? # all characters until <!-- (include)
(?: !-- \K | [^<]*\z\K (*ACCEPT) ) # new comment or end of the string
(?|
-*+ ([^->][^-]*) # possible hyphens not followed by >
|
-+ (?=-->) # hyphens before closing sequence, break the contiguity
|
-?+ ([^-]+) # one hyphen followed by >
|
-->[^<]* (*SKIP)(*FAIL) () # closing sequence, go to next <, break contiguity
) # () avoids a misterious bug in regex101, you can remove it
)
").unwrap();
let string = "<!----------------- toto -->
<!-- toto ---------------------------->
<!---->
<!----->
<!--->>>>--->
asdsd sadasdsad sadasd asdasd asdasd --> asd asdasd
<!--- ->>>>>>
---------------------------------------
***************************************
AUGMENTATION BLUEPRINTS
***************************************
---------------------------------------
--->----
<!-- toto-toto -->
<!--OTHER IDEAS --
<anytaghere>
<!----PROBABLY NOT:---
<whatever>
--><!--";
let substitution = "\\1";
// result will be a String with the substituted value
let result = regex.replace_all(string, substitution);
println!("{}", result);
}
Please keep in mind that these code samples are automatically generated and are not guaranteed to work. If you find any syntax errors, feel free to submit a bug report. For a full regex reference for Rust, please visit: https://docs.rs/regex/latest/regex/