SE-0357: Regex String Processing Algorithms

Over at the other review thread for SE-0354 Ben motivated me to try the new bare regex syntax. While I was playing around with the excellent new APIs, I also tried the replace/replacing functions proposed here.

This worked well:

let str = "A banana a day keeps the doctor away."
let result = str.replacing(/A banana/, with: "Any fruit") // Any fruit a day keeps the doctor away.

The following did not. I wish regex based substitution was also possible, like this:

let result = str.replacing(/A banana (.*)/, with: /Any fruit \1/) // with \1 or $1

(I'm not sure which proposal this feature belongs to, whether this is only a new string processing algorithm or it is something that requires deeper support on the Regex type itself)

3 Likes