replace() and replace-with()

Dimitre's concerns about the naming of the proposed replace-with() function could also be addressed by combining replace() and replace-with() into a single new fn:replace#2 using keyword parameters:

replace($input, pattern:="[0-9]+", replacement:="#$0")

replace($input, pattern:="[0-9]+", replacement:="#$0", flags="ms")

replace($input, pattern:="[0-9]+", using:=format-integer(?, 'Ww'))

which might open the door to having a replacement function with access to matched subgroups:

replace($input, pattern:="^([0-9]+)/([0-9]+)/([0-9]+)$", process-groups="map{
   1: format-integer(?, '00'),
   2: map{'1': 'January', '2': 'February', ...}
   3: format-integer(?, '0000')

(the value of process-groups is a function that accepts a group number as input and returns a function used for processing that group; in this example we exploit the fact that a map is a function).

Michael Kay

Received on Thursday, 3 December 2020 07:19:26 UTC