RegExp type

Undocumented

Methods

-(s:Str, r:RegExp) Source: stdlib.ngs:8448
Returns the string with one occurrence of regexp cut out of it. Throws InvalidArgument if s does not contain r.

Returns

Str

Example

"abc01def23" - /[0-9]+/  # "abcdef23"
.(regexp:RegExp, field:Str)
Get fields of a RegExp. Throws FieldNotFound if field is not one of the allowed values.

Parameters

field"pattern", "options", "arg_options", "names", or "newline"

Returns

Str for "pattern". Int for "options" (effective options), "arg_options" (options passed to PCRE2_COMPILE(3)), and "newline". Hash of names/indexes of named groups for "names".

Example

/abc/i.pattern  # abc
/abc/i.options  # 8 - case insensitive (C_PCRE2_CASELESS)
/^abc/i.arg_options  # 8 - .options has C_PCRE2_ANCHORED too
/(?P<name1>abc)/i.names  # Name to index Hash: {name1=1}
=~(x:Any, r:RegExp, mc:MatchContext)internal Source: stdlib.ngs:8485
Checks for RegExp match. Updates MatchContext.

Returns

Checks whether the argument is of type Str and matches (~) r

Example

%[w1 w2].filter(/1/)  # %[w1]
[](s:Str, r:RegExp) Source: stdlib.ngs:8473
Get substring of a string that corresponds to first match of given regular expression

Example

"x10ab20c30y"[/[0-9]+/]  # "10"
assert_output_has(pp:ProcessesPipeline, expected:RegExp, title:Str=null) Source: autoload/globals/test.ngs:186
Assert process has given output. Throws TestFail.

Example

assert_output_has($(echo abc), /xyz/)        # Throws TestFail
p = $(echo abc); assert_output_has(p, /c$/)  # OK
c_pcre2_match(regexp:RegExp, subject:Str, offset:Int, options:Int)
Search string for regular expression. Uses PCRE2_MATCH(3). Do not use this function directly!

Returns

Int or Arr of Int
code(r:RegExp) Source: stdlib.ngs:8523
Convert a RegExp to NGS code that would produce the RegExp when executed.

Returns

Str

Example

code(/a\/b/i)  # /a\/b/i
replace(s:Str, r:RegExp, mapper:Fun) Source: stdlib.ngs:8504
Replace all occurrences of r

Parameters

mapperFunction that will be called with one matching string at a time that provides the replacement

Returns

Str

Example

"x10ab20c30y".replace(/[0-9]+/, F(match_text) "[$match_text]")  # "x[10]ab[20]c[30]y"
replace(s:Str, r:RegExp, replacement:Str) Source: stdlib.ngs:8514
Undocumented
split(s:Str, r:RegExp) Source: stdlib.ngs:8458
Split string by regexp

Returns

Arr of Str

Example

"x10ab20c30y".split(/[0-9]+/).join(" :: ")  # "x :: ab :: c :: y"
Str(r:RegExp) Source: stdlib.ngs:8531
Regular expression literal, same as code(r)

Returns

Str
without(s:Str, r:RegExp) Source: stdlib.ngs:8465
Get string with all occurrences of r removed

Returns

Str

Example

"x10ab20c30y".without(/[0-9]+/)  # "xabcy"
~(s:Str, r:RegExp, offset:Int=0, options:Int=0) Source: stdlib.ngs:8299
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 PCRE2_MATCH(3).

Returns

MatchResult

Example

globals().keys().filter(/^C_PCRE/)  # lists possible options
"xabcy" ~ /a(.)c/  # <MatchSuccess matches=[abc,b] pattern=/a(.)c/ 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
~~(s:Str, r:RegExp, collect_unmatched:Bool=false) Source: stdlib.ngs:8340
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"