Implement scan/2 filter (#1961)

This commit is contained in:
itchyny authored and GitHub committed 2023-07-04 07:48:29 +09:00
1 parent 83f375cc83
commit 4dc2a2fb9d
2 files changed
+9 -4

No files matched your search

+5 -4
View File
@@ -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) > 1 then capture($val[0]; $val[1])
elif $vt == "array" and ($val | length) > 0 then capture($val[0]; null) elif $vt == "array" and ($val | length) > 0 then capture($val[0]; null)
else error( $vt + " not a string or array") end; else error( $vt + " not a string or array") end;
def scan(re): def scan($re; $flags):
match(re; "g") match($re; "g" + $flags)
| if (.captures|length > 0) | if (.captures|length > 0)
then [ .captures | .[] | .string ] then [ .captures | .[] | .string ]
else .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), # If input is an array, then emit a stream of successive subarrays of length n (or less),
# and similarly for strings. # and similarly for strings.
+4
View File
@@ -126,6 +126,10 @@ gsub("^.*?a"; "b")
["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"],[", ",", ",", "]],[[":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 # reference to named captures
gsub("(?<x>.)[^a]*"; "+\(.x)-") gsub("(?<x>.)[^a]*"; "+\(.x)-")
"Abcabc" "Abcabc"