Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allowing '(' and ')' in regex. #53

Merged
merged 2 commits into from
Nov 22, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading