summaryrefslogtreecommitdiffstats
path: root/builtin.c
diff options
context:
space:
mode:
authorpkoppstein <pkoppstein@gmail.com>2014-07-19 01:08:38 -0400
committerNicolas Williams <nico@cryptonector.com>2014-07-28 19:41:21 -0500
commit53993a994287c86989ab81b02c78e253da9e58d3 (patch)
tree6a974782c47012014d3161fc76f3b8939e283ccb /builtin.c
parente1b20b48f04a1225cd4d541500c1b00eb89a1a78 (diff)
Add capture; document regular expression filters
Fix #493.
Diffstat (limited to 'builtin.c')
-rw-r--r--builtin.c18
1 files changed, 15 insertions, 3 deletions
diff --git a/builtin.c b/builtin.c
index dea5fbbf..238a1fa9 100644
--- a/builtin.c
+++ b/builtin.c
@@ -970,11 +970,23 @@ static const char* const jq_builtins[] = {
"def flatten: reduce .[] as $i ([]; if $i | type == \"array\" then . + ($i | flatten) else . + [$i] end);",
"def flatten(x): reduce .[] as $i ([]; if $i | type == \"array\" and x > 0 then . + ($i | flatten(x-1)) else . + [$i] end);",
"def range(x): range(0;x);",
+ // regular expressions:
"def match(re; mode): _match_impl(re; mode; false)|.[];",
- "def match(val): if val | type == \"string\" then match(val; null) elif val | type == \"array\" and (val | length) > 1 then match(val[0]; val[1]) elif val | type == \"array\" and (val | length > 0) then match(val[0]; null) else error((val | type) + \" not a string or array\") end;",
+ "def match(val): (val|type) as $vt | if $vt == \"string\" then match(val; null)"
+ " elif $vt == \"array\" and (val | length) > 1 then match(val[0]; val[1])"
+ " elif $vt == \"array\" and (val | length) > 0 then match(val[0]; null)"
+ " else error( $vt + \" not a string or array\") end;",
"def test(re; mode): _match_impl(re; mode; true);",
- "def test(val): if val |type == \"string\" then test(val; null) elif val | type == \"array\" and (val | length) > 1 then test(val[0]; val[1]) elif val | type == \"array\" and (val | length > 0) then test(val[0]; null) else error((val | type) + \" not a string or array\") end;",
-// "def test(re): _match(re; null; 1);",
+ "def test(val): (val|type) as $vt | if $vt == \"string\" then test(val; null)"
+ " elif $vt == \"array\" and (val | length) > 1 then test(val[0]; val[1])"
+ " elif $vt == \"array\" and (val | length) > 0 then test(val[0]; null)"
+ " else error( $vt + \" not a string or array\") end;",
+ // Ex.: "a1" | capture( "(?<x>[a-z*])" ).x => "a"
+ "def capture(re; mods): match(re; mods) | reduce ( .captures | .[] | select(.name != null) | { (.name) : .string } ) as $pair ({}; . + $pair);",
+ "def capture(val): (val|type) as $vt | if $vt == \"string\" then capture(val; null)"
+ " elif $vt == \"array\" and (val | length) > 1 then capture(val[0]; val[1])"
+ " elif $vt == \"array\" and (val | length) > 0 then capture(val[0]; null)"
+ " else error( $vt + \" not a string or array\") end;",
// range/3, with a `by` expression argument
"def range(init; upto; by): "
" def _range: "