2015-06-18 19:38:25 -07:00
|
|
|
|
# match builtin
|
|
|
|
|
[match("( )*"; "g")]
|
|
|
|
|
"abc"
|
2023-07-18 08:49:12 +02:00
|
|
|
|
[{"offset":0,"length":0,"string":"","captures":[{"offset":0,"string":"","length":0,"name":null}]},{"offset":1,"length":0,"string":"","captures":[{"offset":1,"string":"","length":0,"name":null}]},{"offset":2,"length":0,"string":"","captures":[{"offset":2,"string":"","length":0,"name":null}]},{"offset":3,"length":0,"string":"","captures":[{"offset":3,"string":"","length":0,"name":null}]}]
|
2015-06-18 19:38:25 -07:00
|
|
|
|
|
|
|
|
|
[match("( )*"; "gn")]
|
|
|
|
|
"abc"
|
|
|
|
|
[]
|
|
|
|
|
|
2023-07-09 15:25:21 +09:00
|
|
|
|
[match(""; "g")]
|
|
|
|
|
"ab"
|
|
|
|
|
[{"offset":0,"length":0,"string":"","captures":[]},{"offset":1,"length":0,"string":"","captures":[]},{"offset":2,"length":0,"string":"","captures":[]}]
|
|
|
|
|
|
2015-06-18 19:38:25 -07:00
|
|
|
|
[match("a"; "gi")]
|
|
|
|
|
"āáàä"
|
|
|
|
|
[]
|
|
|
|
|
|
|
|
|
|
[match(["(bar)"])]
|
|
|
|
|
"foo bar"
|
|
|
|
|
[{"offset": 4, "length": 3, "string": "bar", "captures":[{"offset": 4, "length": 3, "string": "bar", "name": null}]}]
|
|
|
|
|
|
|
|
|
|
# offsets account for combining codepoints and multi-byte UTF-8
|
|
|
|
|
[match("bar")]
|
|
|
|
|
"ā bar with a combining codepoint U+0304"
|
|
|
|
|
[{"offset": 3, "length": 3, "string": "bar", "captures":[]}]
|
|
|
|
|
|
|
|
|
|
# matches with combining codepoints still count them in their length
|
|
|
|
|
[match("bār")]
|
|
|
|
|
"a bār"
|
|
|
|
|
[{"offset": 2, "length": 4, "string": "bār", "captures":[]}]
|
|
|
|
|
|
|
|
|
|
[match(".+?\\b")]
|
|
|
|
|
"ā two-codepoint grapheme"
|
|
|
|
|
[{"offset": 0, "length": 2, "string": "ā", "captures":[]}]
|
|
|
|
|
|
|
|
|
|
[match(["foo (?<bar123>bar)? foo", "ig"])]
|
|
|
|
|
"foo bar foo foo foo"
|
|
|
|
|
[{"offset": 0, "length": 11, "string": "foo bar foo", "captures":[{"offset": 4, "length": 3, "string": "bar", "name": "bar123"}]},{"offset":12, "length": 8, "string": "foo foo", "captures":[{"offset": -1, "length": 0, "string": null, "name": "bar123"}]}]
|
|
|
|
|
|
|
|
|
|
#test builtin
|
|
|
|
|
[test("( )*"; "gn")]
|
|
|
|
|
"abc"
|
|
|
|
|
[false]
|
|
|
|
|
|
|
|
|
|
[test("ā")]
|
|
|
|
|
"ā"
|
|
|
|
|
[true]
|
|
|
|
|
|
|
|
|
|
capture("(?<a>[a-z]+)-(?<n>[0-9]+)")
|
|
|
|
|
"xyzzy-14"
|
|
|
|
|
{"a":"xyzzy","n":"14"}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# jq-coded utilities built on match:
|
|
|
|
|
#
|
|
|
|
|
# The second element in these tests' inputs tests the case where the
|
|
|
|
|
# fromstring matches both the head and tail of the string
|
|
|
|
|
[.[] | sub(", "; ":")]
|
|
|
|
|
["a,b, c, d, e,f", ", a,b, c, d, e,f, "]
|
|
|
|
|
["a,b:c, d, e,f",":a,b, c, d, e,f, "]
|
|
|
|
|
|
|
|
|
|
sub("^(?<head>.)"; "Head=\(.head) Tail=")
|
|
|
|
|
"abcdef"
|
|
|
|
|
"Head=a Tail=bcdef"
|
|
|
|
|
|
|
|
|
|
[.[] | gsub(", "; ":")]
|
|
|
|
|
["a,b, c, d, e,f",", a,b, c, d, e,f, "]
|
|
|
|
|
["a,b:c:d:e,f",":a,b:c:d:e,f:"]
|
|
|
|
|
|
|
|
|
|
gsub("(?<d>\\d)"; ":\(.d);")
|
|
|
|
|
"a1b2"
|
|
|
|
|
"a:1;b:2;"
|
|
|
|
|
|
2015-12-07 18:51:26 -05:00
|
|
|
|
gsub("a";"b")
|
|
|
|
|
"aaaaa"
|
|
|
|
|
"bbbbb"
|
|
|
|
|
|
2023-07-09 15:25:21 +09:00
|
|
|
|
gsub("(.*)"; ""; "x")
|
2015-12-07 18:51:26 -05:00
|
|
|
|
""
|
|
|
|
|
""
|
|
|
|
|
|
2023-07-09 15:25:21 +09:00
|
|
|
|
gsub(""; "a"; "g")
|
2023-07-03 18:46:29 -04:00
|
|
|
|
""
|
|
|
|
|
"a"
|
|
|
|
|
|
2023-07-09 15:25:21 +09:00
|
|
|
|
gsub("^"; ""; "g")
|
2023-07-03 18:46:29 -04:00
|
|
|
|
"a"
|
|
|
|
|
"a"
|
|
|
|
|
|
2023-07-09 15:25:21 +09:00
|
|
|
|
gsub(""; "a"; "g")
|
2023-07-03 18:46:29 -04:00
|
|
|
|
"a"
|
2023-07-09 15:25:21 +09:00
|
|
|
|
"aaa"
|
2023-07-03 18:46:29 -04:00
|
|
|
|
|
2023-07-09 15:25:21 +09:00
|
|
|
|
gsub("$"; "a"; "g")
|
2023-07-03 18:46:29 -04:00
|
|
|
|
"a"
|
|
|
|
|
"aa"
|
|
|
|
|
|
2023-07-09 15:25:21 +09:00
|
|
|
|
gsub("^"; "a")
|
2023-07-03 18:46:29 -04:00
|
|
|
|
""
|
|
|
|
|
"a"
|
|
|
|
|
|
|
|
|
|
gsub("(?=u)"; "u")
|
|
|
|
|
"qux"
|
|
|
|
|
"quux"
|
|
|
|
|
|
|
|
|
|
gsub("^.*a"; "b")
|
|
|
|
|
"aaa"
|
|
|
|
|
"b"
|
|
|
|
|
|
|
|
|
|
gsub("^.*?a"; "b")
|
|
|
|
|
"aaa"
|
|
|
|
|
"baa"
|
|
|
|
|
|
|
|
|
|
# The following is for regression testing and should not be construed as a requirement:
|
|
|
|
|
[gsub("a"; "b", "c")]
|
|
|
|
|
"a"
|
|
|
|
|
["b","c"]
|
|
|
|
|
|
2015-06-18 19:38:25 -07:00
|
|
|
|
[.[] | scan(", ")]
|
|
|
|
|
["a,b, c, d, e,f",", a,b, c, d, e,f, "]
|
|
|
|
|
[", ",", ",", ",", ",", ",", ",", ",", "]
|
|
|
|
|
|
|
|
|
|
[.[]|[[sub(", *";":")], [gsub(", *";":")], [scan(", *")]]]
|
|
|
|
|
["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:"],[", ",",",", ",", ",", ",",",", "]]]
|
|
|
|
|
|
|
|
|
|
[.[]|[[sub(", +";":")], [gsub(", +";":")], [scan(", +")]]]
|
|
|
|
|
["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:"],[", ",", ",", ",", ",", "]]]
|
|
|
|
|
|
2023-07-04 07:48:29 +09:00
|
|
|
|
[.[] | scan("b+"; "i")]
|
|
|
|
|
["","bBb","abcABBBCabbbc"]
|
|
|
|
|
["bBb","b","BBB","bbb"]
|
|
|
|
|
|
2015-06-18 19:38:25 -07:00
|
|
|
|
# reference to named captures
|
|
|
|
|
gsub("(?<x>.)[^a]*"; "+\(.x)-")
|
|
|
|
|
"Abcabc"
|
|
|
|
|
"+A-+a-"
|
2015-08-22 12:18:13 -07:00
|
|
|
|
|
2023-07-03 18:46:29 -04:00
|
|
|
|
gsub("(?<x>.)(?<y>[0-9])"; "\(.x|ascii_downcase)\(.y)")
|
|
|
|
|
"A1 B2 CD"
|
|
|
|
|
"a1 b2 CD"
|
|
|
|
|
|
|
|
|
|
gsub("\\b(?<x>.)"; "\(.x|ascii_downcase)")
|
|
|
|
|
"ABC DEF"
|
|
|
|
|
"aBC dEF"
|
|
|
|
|
|
2023-07-18 08:49:12 +02:00
|
|
|
|
gsub("[^a-z]*(?<x>[a-z]*)"; "Z\(.x)")
|
|
|
|
|
"123foo456bar"
|
|
|
|
|
"ZfooZbarZ"
|
|
|
|
|
|
2015-08-22 12:18:13 -07:00
|
|
|
|
# utf-8
|
|
|
|
|
sub("(?<x>.)"; "\(.x)!")
|
|
|
|
|
"’"
|
|
|
|
|
"’!"
|
2023-07-03 18:46:29 -04:00
|
|
|
|
|
|
|
|
|
[sub("a"; "b", "c")]
|
|
|
|
|
"a"
|
|
|
|
|
["b","c"]
|
|
|
|
|
|
|
|
|
|
[sub("(?<a>.)"; "\(.a|ascii_upcase)", "\(.a|ascii_downcase)", "c")]
|
|
|
|
|
"aB"
|
|
|
|
|
["AB","aB","cB"]
|
|
|
|
|
|
|
|
|
|
[gsub("(?<a>.)"; "\(.a|ascii_upcase)", "\(.a|ascii_downcase)", "c")]
|
|
|
|
|
"aB"
|
|
|
|
|
["AB","ab","cc"]
|
|
|
|
|
|
|
|
|
|
# splits and _nwise
|
|
|
|
|
[splits("")]
|
|
|
|
|
"ab"
|
2023-07-09 15:25:21 +09:00
|
|
|
|
["","a","b",""]
|
2023-07-03 18:46:29 -04:00
|
|
|
|
|