rust-bakery / nom-regex Goto Github PK
View Code? Open in Web Editor NEWLicense: MIT License
License: MIT License
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
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("]"),
)
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.
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.