diff --git a/README.md b/README.md index 2894b6e..1e497ee 100644 --- a/README.md +++ b/README.md @@ -227,7 +227,7 @@ Given the json | `$..book[2:]` | Book number two from tail | | `$.store.book[?(@.price < 10)]` | All books in store cheaper than 10 | | `$..book[?(@.price <= $.expensive)]` | All books in store that are not "expensive" | -| `$..book[?(@.author ~= /.*REES/i)]` | All books matching regex (ignore case) | +| `$..book[?(@.author ~= '(?i)REES')]` | All books matching regex (ignore case) | | `$..*` | Give me every thing | ### The library diff --git a/src/lib.rs b/src/lib.rs index 786a5ae..5d4e8e2 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1227,6 +1227,23 @@ mod tests { assert_eq!(v, vec![NoValue]); } + #[test] + fn regex_filter_test() { + let json: Box = Box::new(json!({ + "author":"abcd(Rees)", + })); + + let path: Box = Box::from( + JsonPathInst::from_str("$.[?(@.author ~= '(?i)d\\(Rees\\)')]") + .expect("the path is correct"), + ); + let finder = JsonPathFinder::new(json.clone(), path); + assert_eq!( + finder.find_slice(), + vec![Slice(&json!({"author":"abcd(Rees)"}), "$".to_string())] + ); + } + // #[test] // fn no_value_len_field_test() { // let json: Box = diff --git a/src/parser/grammar/json_path.pest b/src/parser/grammar/json_path.pest index a0ed959..e6dffe8 100644 --- a/src/parser/grammar/json_path.pest +++ b/src/parser/grammar/json_path.pest @@ -14,7 +14,7 @@ string_qt = ${ "\'" ~ inner ~ "\'" } inner = @{ char* } char = _{ !("\"" | "\\" | "\'") ~ ANY - | "\\" ~ ("\"" | "\'" | "\\" | "/" | "b" | "f" | "n" | "r" | "t") + | "\\" ~ ("\"" | "\'" | "\\" | "/" | "b" | "f" | "n" | "r" | "t" | "(" | ")") | "\\" ~ ("u" ~ ASCII_HEX_DIGIT{4}) } root = {"$"} @@ -24,7 +24,7 @@ key_unlim = {"[" ~ string_qt ~ "]"} key = ${key_lim | key_unlim} descent = {dot ~ dot ~ key} -descent_w = {dot ~ dot ~ "*"} // refactor afterwards +descent_w = {dot ~ dot ~ "*"} // refactor afterwards wildcard = {dot? ~ "[" ~"*"~"]" | dot ~ "*"} current = {"@" ~ chain?} field = ${dot? ~ key_unlim | dot ~ key_lim }