diff --git a/src/builtin.jq b/src/builtin.jq index a102fd51a0..49ea93c47d 100644 --- a/src/builtin.jq +++ b/src/builtin.jq @@ -90,12 +90,13 @@ def capture($val): ($val|type) as $vt | if $vt == "string" then capture($val; nu 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; -def scan(re): - match(re; "g") - | if (.captures|length > 0) +def scan($re; $flags): + match($re; "g" + $flags) + | if (.captures|length > 0) then [ .captures | .[] | .string ] else .string - end ; + end; +def scan($re): scan($re; null); # # If input is an array, then emit a stream of successive subarrays of length n (or less), # and similarly for strings. diff --git a/tests/onig.test b/tests/onig.test index daacae9cd7..a3d80d4ba5 100644 --- a/tests/onig.test +++ b/tests/onig.test @@ -87,6 +87,10 @@ gsub( "(.*)"; ""; "x") ["a,b, c, d, e,f",", a,b, c, d, e,f, "] [[["a,b:c, d, e,f"],["a,b:c:d:e,f"],[", ",", ",", "]],[[":a,b, c, d, e,f, "],[":a,b:c:d:e,f:"],[", ",", ",", ",", ",", "]]] +[.[] | scan("b+"; "i")] +["","bBb","abcABBBCabbbc"] +["bBb","b","BBB","bbb"] + # reference to named captures gsub("(?.)[^a]*"; "+\(.x)-") "Abcabc"