Skip to content

Commit

Permalink
Allowing '(' and ')' in regex. (#53)
Browse files Browse the repository at this point in the history
* Allowing '(' and ')' in regex using '\\(' and '\\)'.
Changed regex syntax in README to match rust regex.
Added a test for regex.

* rustfmt fail fix
  • Loading branch information
catalinstochita committed Nov 22, 2023
1 parent 76cf9d2 commit c7b5524
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 3 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
17 changes: 17 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1227,6 +1227,23 @@ mod tests {
assert_eq!(v, vec![NoValue]);
}

#[test]
fn regex_filter_test() {
let json: Box<Value> = Box::new(json!({
"author":"abcd(Rees)",
}));

let path: Box<JsonPathInst> = 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<Value> =
Expand Down
4 changes: 2 additions & 2 deletions src/parser/grammar/json_path.pest
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {"$"}
Expand All @@ -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 }
Expand Down

0 comments on commit c7b5524

Please sign in to comment.