RegExp type

Undocumented

Methods

-(s:Str, r:RegExp) Source: stdlib.ngs:5536
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. You should not use this directly. Use "~" and "~~" operators.

Parameters

field"options" or "names"

Returns

Int for "options". Hash of names/indexes of named groups for "names".

Example

/abc/i.options  # 1 - case insensitive (C_PCRE_CASELESS)
/(?P<name1>abc)/i.names  # Name to index Hash: {name1=1}
[](s:Str, r:RegExp) Source: stdlib.ngs:5562
Get substring of a string that corresponds to first match of given regular expression

Example

"x10ab20c30y"[/[0-9]+/]  # "10"
assert_output_has(cp:CommandsPipeline, expected:RegExp, title:Str=null) Source: autoload/test.ngs:178
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_pcre_exec(regexp:RegExp, subject:Str, offset:Int, options:Int)
Search string for regular expression. Uses PCRE_EXEC(3). Do not use this function directly!

Returns

Int or Arr of Int
Pred(r:RegExp) Source: stdlib.ngs:5571
Convert regular expression to a predicate.

Returns

A function that checks whether the only argument (of type Str) matches r

Example

%[w1 w2].filter(/1/)  # %[w1]
replace(s:Str, r:RegExp, mapper:Fun) Source: stdlib.ngs:5580
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"
split(s:Str, r:RegExp) Source: stdlib.ngs:5546
Split string by regexp

Returns

Arr of Str

Example

"x10ab20c30y".split(/[0-9]+/).join(" :: ")  # "x :: ab :: c :: y"
Str(regexp:RegExp)
Represents RegExp

Returns

The string <RegExp>
without(s:Str, r:RegExp) Source: stdlib.ngs:5554
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:5401
Find PCRE regular expression in s. Empty string without options returns MatchN. 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

Match

Example

globals().keys().filter(/^C_PCRE/)  # lists possible options
"xabcy" ~ /a(.)c/  # <MatchY 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
~~(s:Str, r:RegExp, collect_unmatched:Bool=false) Source: stdlib.ngs:5444
Find all non-overlapping matches of regular expression in a string.

Returns

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

Example

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

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