I'm trying to match the two characters after a specific character. The trailing values may contain the specified character, which is ok, but I also need to capture that specified character as the beginning of the next capture group.
This code should illustrate what I mean:
extern crate regex;
use regex::Regex;
pub fn main() {
let re = Regex::new("(a..)").unwrap();
let st = String::from("aba34jf baacdaab");
println!("String to match: {}", st);
for cap in re.captures_iter(&st) {
println!("{}", cap[1].to_string());
// Prints "aba" and "aac",
// Should print "aba", "a34", "aac", "acd", "aab"
}
}
How do I get overlapping captures without using look around (which the regex crate doesn't support in Rust)? Is there something similar to what is in Python (as mentioned here) but in Rust?
Edit:
Using onig as BurntSushi5 suggested, we get the following:
extern crate onig;
use onig::*;
pub fn main() {
let re = Regex::new("(?=(a.{2}))").unwrap();
let st = String::from("aba34jf baacdaab");
println!("String to match: {}", st);
for ch in re.find_iter(&st) {
print!("{} ", &st[ch.0..=ch.1+2]);
// aba a34 aac acd aab, as it should.
// but we have to know how long the capture is.
}
println!("");
}
Now the problem with this is that you have to know how long the regex is, because the look ahead group doesn't capture. Is there a way to get the look ahead regex captured without knowing the length beforehand? How would we print it out if we had something like (?=(a.+))
as the regex?