~~ multimethod

Methods

~~(haystack:Str, needle:Str, collect_unmatched:Bool=false) Source: stdlib.ngs:3679
Find all non-overlapping matches of a substring in a string.

Returns

if collect_unmatched - Arr with each element being MatchSuccess or Str, if not collect_unmatched - Arr of MatchSuccess

Example

["key=value", "key2=value2"].map(F(kv) {
  m = kv ~ "="
  [m.before, m.after]
}).Hash()  # {'key': 'value', 'key2': 'value2'}
~~(s:Str, r:RegExp, collect_unmatched:Bool=false) Source: stdlib.ngs:5756
Find all non-overlapping matches of regular expression in a string.

Returns

if collect_unmatched - Arr with each element being MatchSuccess or Str, if not collect_unmatched - Arr of MatchSuccess

Example

("x10ab20c30y" ~~ /[0-9]+/).whole.map(Int).sum()  # 60

arr = (~~)("x10ab20c30y", /[0-9]+/, true)
arr .= map(F(elt) if elt is MatchSuccess "[${elt.whole}]" else elt)
arr.join("")  # "x[10]ab[20]c[30]y"