I take it back, captures_at still isn't sufficient for all cases.
In the case of regex which can start with context sensitive assertions (like \b), you would need \A(?:original_regex) to match on the substring, and then to run the original regex again with captures_read_at to confirm (which would not be anchored and may run on). Example.
So I think there's currently no efficient way in the most general case without a captures_at_exactly or similar.
If you don't have such leading context sensitivity, I believe you can use the anchored version on a substring approach. (E.g. remove the \b in my playground and the two runs will agree; you can use "naive anchoring".)
...for the Regex crate.
@rauschma, if you're using fancy_regex (which has different maintainers), it looks like they support the \G anchor. I think that will do what you want, at least, it looked like sticky was the same as adding \G to the beginning of your regex to me. (It's not on the playground so it's not easy for me to check right now.)
I also have no idea how they implemented it (e.g. efficiency-wise).
use fancy_regex::Regex;
fn main() {
let re = Regex::new(r"\G((?P<emph>_)|(?P<ex>x))").unwrap();
let text = "_x x";
let mut index = 0usize;
loop {
match re.captures_from_pos(text, index).expect("TODO") {
None => {break}
Some(captures) => {
let m = captures.get(0).unwrap();
println!("TOKEN: {}", &text[m.start()..m.end()]);
index = m.end();
}
}
}
}