-1
Rustにテキスト入力を生のリテラル文字列として解釈させるにはどうすればよいですか?私は正規表現の入力を取るRegex
検索機能を作成し、いくつかのテキストを検索するためにそれを使用しようとしています:私はRegex
のためにそれを使用することはできませんので、入力をRustの生の文字列として設定する方法は?
...
fn main() {
// Initiate file to search through
let text_path = Path::new("test.txt");
let mut text_file = File::open(text_path).unwrap();
let mut text = String::new();
text_file.read_to_string(&mut text);
// Search keyword
let mut search_keyword = String::new();
// Display filename and ask user for Regex
print!("Search (regex) in file[{path}]: ", path=text_path.display());
io::stdout().flush().ok();
// Get search keyword
io::stdin().read_line(&mut search_keyword).unwrap();
println!("You are searching: {:?}", search_keyword);
let search = to_regex(&search_keyword.trim()).is_match(&text);
println!("Contains search term: {:?}", search);
}
fn to_regex(keyword: &str) -> Regex {
Regex::new(keyword).unwrap()
}
錆が自動的に入力をエスケープします。私はあなたが文字列のためにこれを行うことができることを知っています:
r"Some text here with with escaping characters: \ "
しかし、私はどのように変数でそれを使うことができますか?
おそらく 'r#" somestring "#"をやってみてください... –