I'm struggling to figure out how to capture a string which breaks across two lines. I've put a sample on the Playground. If you delete the \n character and have the string on the same line, it works. But with the \n it doesn't work. I've added the r"(?s) so it should in theory treat a \n as a character.
Any help would be greatly appreciated.
use regex::Regex;
fn main() {
let msting = " 24.
Kh1 Qg2# {+1000.01/28}";
let rxmove = Regex::new(r"(?s)[[:space:]]([[:digit:]]+[.\n.?])[[:space:]]([[:word:]]+)[[:space:]]([[:word:]]+)?").unwrap();
for cap in rxmove.captures_iter(msting).into_iter()
{
println!("One: {} Two: {} Three: {}", &cap[1], &cap[2], &cap[3]);
}
}
You're not using the . metacharacter, so you don't need this. . in a character class is just a literal . (and you have \n in the character class anyway)
No need to have . twice in a character class. Or, if you meant "maybe any character", note that . and ? are both literal within a character class. Maybe you meant [.].? (with (?s))? Unclear.
You only allow for a single whitespace character between the first capture and the second capture. Maybe you meant [[:space:]]* or [[:space:]]+ to allow 0 or more, or 1 or more.
If you're purposefully only allowing one space, probably it's your string that's the problem, like @steffahn said.