Nim regex to perform once not global replace

Viewed 96

How do we ask Nim regex to perform once not global replacing it with a specific string (e.g. let sided by Perl syntax) ?

import std/nre

var o="foo bar blah !"
    
echo o.replace( re"(\w+)", "masked")

it'd be:

$o='foo bar blah !' ;
$o =~ s/(\w+)/masked/g ;
print $o

masked masked masked !

not the expected

masked bar blah !
1 Answers

the api for this is not currently available in standard library's modules re and nre but it is available in nim-regex using limit optional parameter (see docs):

import regex

var o = "foo bar blah !"
    
echo o.replace(re"(\w+)", "masked", limit=1)
# masked bar blah !
Related