Hello!
I've recently started to learn Rust and I'm having some issues regarding regex. So i wanted my program to read the contents of the file and check if it matches the regex.
so heres the content of the file:
a = point ( 1 , 1 ) ;
b = point ( 2 , 2 ) ;
c = point ( 3 , 3 ) .
And here is my Rust program. sorry for the messy regex
use std::env;
use std::fs::File;
use std::io::prelude::*;
extern crate regex;
use regex::Regex;
fn main() -> std::io::Result<()>{
let args: Vec<String> = env::args().collect();
let reg : Regex = Regex::new(r"(?s)^[a-zA-z]+[[:space:]]*=[[:space:]]*point[[:space:]]*\([[:space:]]*[0-9]+[[:space:]]*,[[:space:]]*[0-9]+[[:space:]]*\)[[:space:]]*;\n[a-zA-z]+[[:space:]]*=[[:space:]]*point[[:space:]]*\([[:space:]]*[0-9]+[[:space:]]*,[[:space:]]*[0-9]+[[:space:]]*\)[[:space:]]*;\n[a-zA-z]+[[:space:]]*=[[:space:]]*point[[:space:]]*\([[:space:]]*[0-9]+[[:space:]]*,[[:space:]]*[0-9]+[[:space:]]*\)[[:space:]]*\.$").unwrap();
let text = "a = point ( 1 , 1 ) ;
b = point ( 2 , 2 ) ;
c = point ( 3 , 3 ) .";
let mut file = File::open(&args[1])?; //specify filename in argument
let mut contents = String::new();
file.read_to_string(&mut contents)?;
let contents_result = reg.is_match(&contents);
let text_result = reg.is_match(text);
if contents_result == true{
println!("success");
}
else{
println!("fail");
}
if text_result == true{
println!("success");
}
else{
println!("fail");
}
Ok(())
}
So the string inside text and the string inside the file is identical, But in the result matching, the text succeeded while the string from the file fails. Is there something im missing? Thank you for your time ![]()