RegExp type
Methods
- -(s:Str, r:RegExp) Source: stdlib.ngs:7179
Returns the string with one occurrence of regexp cut out of it. Throws InvalidArgument if s does not contain r. Returns
StrExample
"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}
- =~(x:Any, r:RegExp, _mc:MatchContext) Source: stdlib.ngs:7214
Undocumented Returns
Checks whether the only argument is of type Str and matches (~) rExample
%[w1 w2].filter(/1/) # %[w1]
- [](s:Str, r:RegExp) Source: stdlib.ngs:7204
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)
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
- replace(s:Str, r:RegExp, mapper:Fun) Source: stdlib.ngs:7226
Replace all occurrences of r Parameters
mapper Function that will be called with one matching string at a time that provides the replacement Returns
StrExample
"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:7236
Undocumented
- split(s:Str, r:RegExp) Source: stdlib.ngs:7189
Split string by regexp Returns
Arr of StrExample
"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:7196
Get string with all occurrences of r removed Returns
StrExample
"x10ab20c30y".without(/[0-9]+/) # "xabcy"
- ~(s:Str, r:RegExp, offset:Int=0, options:Int=0) Source: stdlib.ngs:7045
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
offset search start offset options options to pass to underlying PCRE_EXEC(3). Returns
MatchResultExample
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
- ~~(s:Str, r:RegExp, collect_unmatched:Bool=false) Source: stdlib.ngs:7089
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 MatchSuccessExample
("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"