Capture A Match That Is Not Followed By A String In Rust's nom
January 2024
The goal of this parser is to match everything up to a specific string. Right no it returns a Vec of &str that can be joined. I'm trying to figure out if there's a way to flatten it and return a single &str.
I don't have a specific need to get to the &str. Mainly I'm curious to see if it can be done and thereby keep &str all the way up. If not, I'll do the conversion of the final Vec into a String for the return value of get_up_to()
```cargo
[dependencies]
nom ="7.1.3"
```
usenom::bytes::complete::tag_no_case;usenom::bytes::complete::take_until;usenom::combinator::not;usenom::sequence::pair;usenom::sequence::terminated;usenom::IResult;fnget_up_to<'a>(source:&'astr, target:&'astr)->IResult<&'astr, Vec<&'astr>>{letmut result =vec![];let(source, captured)=(|src|get_part(src, target))(source)?;
result.push(captured.0);
result.push(captured.1);Ok((source, result))}fnget_part<'a>(source:&'astr, target:&'astr)->IResult<&'astr, (&'astr, &'astr)>{let(source, captured)=terminated(pair(take_until(target),tag_no_case(target)),not(tag_no_case(target)),)(source)?;Ok((source, captured))}fnmain(){let tests =vec![("alfa bravo charlie","bravo"," charlie","alfa bravo"),];for test in tests.iter(){let result =get_up_to(test.0, test.1).unwrap();assert_eq!(result.0, test.2,"Check remainder");assert_eq!(result.1.join(""), test.3,"Check captured");}println!("All Tests Passed");}