Note: This site is currently "Under construction". I'm migrating to a new version of my site building software. Lots of things are in a state of disrepair as a result (for example, footnote links aren't working). It's all part of the process of building in public. Most things should still be readable though.

Match A String Until A Case Insensitive Tag With nom In Rust

See the second one if you want to keep the text

Code

//! ```cargo
//! [dependencies]
//! nom = "7.1.3"
//! ```

use nom::IResult;
use nom::multi::many_till;
use nom::character::complete::anychar;
use nom::bytes::complete::tag_no_case;

fn main() {
    let source = "the quick brown fox";
    let result = parse(source).unwrap().1;
    println!("{:?}", result);
}

fn parse(source: &str) -> IResult<&str, String> {
    let (source, result) = many_till(anychar, tag_no_case(" brown"))(source)?;
    let result: String = result.0.iter().collect();
    Ok((
        source,
        result
    ))
}

Results

"the quick"

This is aimed at keeping the ending part

Code

//! ```cargo
//! [dependencies]
//! nom = "7.1.3"
//! ```

use nom::IResult;
use nom::multi::many_till;
use nom::character::complete::anychar;
// use nom::bytes::complete::tag_no_case;
// use nom::sequence::pair;
// use nom::bytes::complete::take_until;
use nom::bytes::complete::tag;
use nom::combinator::peek;

fn main() {
    let source = "the quick brown fox";
    let result = parse(source);
    dbg!(result.unwrap());
}

fn parse(source: &str) -> IResult<&str, String> {
    let (source, result) = many_till(anychar, peek(tag(" brown")))(source)?;
    let result: String = result.0.iter().collect();
    Ok((
        source,
        result
    ))
}

Results

[_active_nvim_run:18] result.unwrap() = (
    " brown fox",
    "the quick",
)