~ multimethod

Methods

~(something:Any, pred:Any)deprecated Source: stdlib.ngs:954
EXPERIMENTAL! Do not use! Especially don't count on returned value beyond obvios Bool(x) behaviour. Match anything.
~(arr:Arr, r:PredRange) Source: stdlib.ngs:2126
Check whether the array contains the given range. Finds first match.

Returns

MatchResult

Example

a = %[a1 a2 b1 b2]
m = a ~ /^a/../^b/ # Exclusive range
# m.matches == [['a2']]
# m.before == ['a1']
# m.after == ['b1', 'b2']
~(haystack:Str, needle:Str, offset:Int=0) Source: stdlib.ngs:3660
Find substring in string. Uses pos().

Returns

MatchResult

Example

"abc" ~ "bc"  # <MatchSuccess ...>
"abc" ~ "X"   # <MatchFailure>
~(s:Str, pfx:Pfx) Source: stdlib.ngs:5667
Check whether s starts with pfx.

Returns

MatchResult

Example

"abcde" ~ Pfx("ab")  # <MatchSuccess matches=['ab'] before= after=cde>
"abcde" ~ Pfx("xy")  # <MatchFailure >
~(s:Str, i:Ifx) Source: stdlib.ngs:5676
Check whether s contains i. Same as s ~ i.val

Returns

MatchResult
~(s:Str, sfx:Sfx) Source: stdlib.ngs:5683
Check whether s ends with sfx.

Returns

MatchResult

Example

"abcde" ~ Sfx("de")  # <MatchSuccess matches=['de'] before=abc after=>
"abcde" ~ Pfx("xy")  # <MatchFailure >
~(s:Str, r:RegExp, offset:Int=0, options:Int=0) Source: stdlib.ngs:5713
Find PCRE regular expression in s. Empty string without options returns MatchFailure. Throws Error if more than 20 captures are used or if there is an error during matching.

Automatically called by NGS for syntax

my_str ~ my_regexp

Parameters

offsetsearch start offset
optionsoptions to pass to underlying PCRE_EXEC(3).

Returns

MatchResult

Example

globals().keys().filter(/^C_PCRE/)  # lists possible options
"xabcy" ~ /a(.)c/  # <MatchSuccess matches=['abc','b'] named={} positions=[[1,4],[2,3]] whole=abc before=x after=y>
m = ("xabcy" ~ /a(?P<mychar>.)c/)
echo(m.named.mychar)  # Outputs: b
~(argv:Arr, udm:UserDefinedMethod) Source: autoload/ArgvMatcher.ngs:51
Please do not use directly! Tries to match command line arguments with closure parameters.

Parameters

argvCommand line arguments, typically ARGV
udmUserDefinedMethod to match with

Returns

Match (ParamsMatchY on success, ParamsMatchN on failure). If ParamsMatchY is returned it has "matches" field with values in suitable order to call c. If ParamsMatchN is returned, it has "message" field explaining why there was no match. Currently it's not printed anywhere.

Example

ArgvMatcher; (["--b", "B", "A", "C"] ~ F(a,b,c) 7).matches  # %[A B C]
(["A", "C", "D", "--b", "B"] ~ ArgvMatcher("positionals", "a") do F(a,b) 7).matches  # %[%[A C D] B]