~ multimethod

Methods

~(x:Any, a:AnyOf) Source: stdlib.ngs:1960
Checks whether x matches (~) any of the alternatives.
~(arr:Arr, r:PredRange) Source: stdlib.ngs:3424
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:5067
Find substring in string. Uses pos().

Returns

MatchResult

Example

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

Returns

MatchResult

Example

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

Returns

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

Returns

MatchResult

Example

"abcde" ~ Sfx("de")  # <MatchSuccess matches=[de] pattern=<MustSfx de> before=abc after=>
"abcde" ~ Pfx("xy")  # <MatchFailure >
~(s:Str, r:RegExp, offset:Int=0, options:Int=0) Source: stdlib.ngs:7626
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] pattern=<RegExp> named={} positions=[<NumRange 1..4 include_start=true include_end=false step=1>,<NumRange 2..3 include_start=true include_end=false step=1>] whole=abc before=x after=y>
m = ("xabcy" ~ /a(?P<mychar>.)c/)
echo(m.named.mychar)  # Outputs: b
~(argv:Arr, udm:UserDefinedMethod)
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]