// 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"(?m)(?:\bclass \w+\s*{\s*|\G(?!\A)\s*(?:(?://|#).*$|/\*[\s\S]*?\*/|\bconst[^;]*;)\s*)\K(\$\w+\s*(?:;|=.*;))").unwrap();
let string = "class MyClass {
$myvar1;
// some comment here (even with a variable $x;)
$myvar2;
/* I have a multiline comment here
with a variable $x; here
*/
$myvar3;
# Just another comment $x;
$myvar4;
const SOMETHING;
$myvar5;
function __construct($myvar1 = NULL, $myvar2 = array()) {
$this->myvar1 = $myvar1;
$this->myvar2 = $myvar2;
}
}";
let substitution = "private $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/