// 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#"(?xs)(?(DEFINE)
(?<next_open_tag>
[^<]*+
(?i: <++[^<?s][^<]*
| <++(?! \?php
| \?=
| script\s*language\s*=\s*([\'"]?)php\g{-1}\s*>
) [^<]*
)*+
(?i: <++(?: \?php
| \?=
| [^>]+
)
| \z
)
)
)
\A (?&next_open_tag) \K
|
[^'"`/#<?]*+
(?: '(?:[^'\\]+|\\.)*+' [^\'"`/#<?]*
| "(?:[^"\\]+|\\.)*+" [^\'"`/#<?]*
| `(?:[^`\\]+|\\.)*+` [^\'"`/#<?]*
| /(?![/*]) [^\'"`/#<?]* # stop for // or /*
| # if close tag ?>
\? (?: >(?&next_open_tag)[^\'"`/#<?]* | )
| < (?: # heredoc or nowdoc
<<[\ \t]*([\'"]?)
([a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*)
\g{-2}[\ \t]*[\r\n]
(?-s:.*+[\r\n])*?
\g{-1}[\r\n;]
[^\'"`/#<?]*
| (?i: /script\s*>)
(?&next_open_tag)
[^\'"`/#<?]*
| [^\'"`/#<?]*
)
)*+
\K
(?: (?://|\#)(?:[^\n?]+|\?(?!>))*+ # single line comment // и #
| /\*(?:[^*]+|\*(?!/))*+\*/ # multi line comment /* */
)?
"#).unwrap();
let string = "<!-- *//* -->
<?php
$a = \"text */*\"; // comment
$b = 'text *//*'; # comment
//*
if ($a) {
echo $b;
}
/*/
if ($bar) {
echo $foo;
}
// */
?>
<!-- *//* -->
<script language = \"php\" >
# comment ?> Text <?php $a = '/*'; // comment
$b = @`ls ./* -al`;
# */
</script>
<!-- *//* -->
";
let substitution = "";
// 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/