我想编写一个匹配文件系统路径的 PEG。路径元素是 posix linux 中除 /
之外的任何字符。
PEG 中有一个表达式可以匹配任何
字符,但我不知道如何匹配除一个字符以外的任何字符。
我使用的 peg 解析器是 PEST for rust。
最佳答案
您可以在 https://docs.rs/pest/0.4.1/pest/macro.grammar.html#syntax 中找到 PEST 语法,特别是有一个“负面前瞻”
!a
— matches ifa
doesn't match without making progress
所以你可以这样写
!["/"] ~ any
例子:
// cargo-deps: pest
#[macro_use] extern crate pest;
use pest::*;
fn main() {
impl_rdp! {
grammar! {
path = @{ soi ~ (["/"] ~ component)+ ~ eoi }
component = @{ (!["/"] ~ any)+ }
}
}
println!("should be true: {}", Rdp::new(StringInput::new("/bcc/cc/v")).path());
println!("should be false: {}", Rdp::new(StringInput::new("/bcc/cc//v")).path());
}
关于parsing - 解析器表达式语法 - 如何匹配不包括单个字符的任何字符串?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43269689/