Giter Site home page Giter Site logo

nom-regex's People

Contributors

geal avatar

Stargazers

 avatar

Watchers

 avatar  avatar

Forkers

jkachmar tmccombs

nom-regex's Issues

Nested groups leave unexpected input in remaining to parse

Expectation with scenario 1 is that the remaining to parse string does not container either of the / as they should be consumed by the outer group.

use nom::error::ErrorKind;
use nom_regex::str::re_captures;
fn main() {
  println!("{}", "Broken / still in remaining to parse");
  let re = regex::Regex::new(r"^([/](.+)[/])").unwrap();
  let parser = re_captures::<(&str, ErrorKind)>(re);
  println!("{:?}",  parser("/pr[]-/ && abc"));

  println!("{}", "Works but needs post processing");
  let re = regex::Regex::new(r"^([/].+[/])").unwrap();
  let parser = re_captures::<(&str, ErrorKind)>(re);
  println!("{:?}",  parser("/pr[]-/ && abc"));

  println!("{}", "/ still in remaining to parse");
  let re = regex::Regex::new(r"^[/](.+)[/]").unwrap();
  let parser = re_captures::<(&str, ErrorKind)>(re);
  println!("{:?}",  parser("/pr[]-/ && abc"));

}

repl.it https://replit.com/@merc1031/IllAttentiveDrivers#src/main.rs

Feature request: re_take_until

This is a continuation of rust-bakery/nom#709, since regex nom functions have since moved into this crate.

I have a specific use case: I'd like to parse "[thing1, thing2part1,part2, thing3]" into ["thing1", "thing2part1,part2", "thing3"].

If I could guarantee no item-internal commas, I could implement this as:

delimited(
    tag("["),
    separated_list0(tag(", "), take_while(|c| ![',', ']'].contains(&c))),
    tag("]"),
)

However, since I want to parse a multi-letter sequence, this won't work.

I could do something with take_till, and probably will for this case, but I think that ends up pretty hacky.

On the other hand, regex engines are optimized for this kind of matching, so I believe using regex (or anything aho_corasick-based) is the best option.

I'd propose re_take_until, matching the interface of complete::take_until except taking in a regex rather than byte/string directly.

It'd be used like this to match the above pattern:

let closing_bracket_or_comma_space = regex::Regex::new(r"]|, ").unwrap();
delimited(
    tag("["),
    separated_list0(tag(", "), re_take_until(closing_bracket_or_comma_space),
    tag("]"),
)

re_captures returns residue

Hey,

I'm seeing an issue, please see the following code:

fn main() {
    let re = regex::Regex::new(
        r#"Mem: (\d+)K used, (\d+)K free, (\d+)K shrd, (\d+)K buff, (\d+)K cached"#,
    )
    .unwrap();
    let i = "Mem: 19903688K used, 4720664K free, 38016K shrd, 1963812K buff, 13986644K cached";
    let (io, captures) =
        nom_regex::str::re_captures::<nom::error::VerboseError<&str>>(re)(i).unwrap();

    assert_eq!(captures.len(), 1);
    assert_eq!(captures[0][0], i);
    println!("io: '{io}'");
    assert!(io.is_empty());
}
cargo r -q
io: 'K cached'
thread 'main' panicked at 'assertion failed: io.is_empty()', src/main.rs:13:5

I would expect io to be empty after the re_capture call since captures[0][0] does indeed contain the entire input.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.