parsing - Parser expression grammar - how to match any string excluding a single character? -
i'd write peg matches filesystem paths. path element character except /
in posix linux.
there expression in peg match any
character, cannot figure out how match character except one.
the peg parser i'm using pest rust.
you find pest syntax in https://docs.rs/pest/0.4.1/pest/macro.grammar.html#syntax, in particular there "negative lookahead"
!a
— matches ifa
doesn't match without making progress
so write
!["/"] ~
example:
// cargo-deps: pest #[macro_use] extern crate pest; use pest::*; fn main() { impl_rdp! { grammar! { path = @{ soi ~ (["/"] ~ component)+ ~ eoi } component = @{ (!["/"] ~ any)+ } } } println!("should true: {}", rdp::new(stringinput::new("/bcc/cc/v")).path()); println!("should false: {}", rdp::new(stringinput::new("/bcc/cc//v")).path()); }
Comments
Post a Comment