nitely / nim-regex

Pure Nim regex engine. Guarantees linear time matching
https://nitely.github.io/nim-regex/
MIT License
227 stars 20 forks source link

Feature request: Uppercase replacements #44

Open jyapayne opened 4 years ago

jyapayne commented 4 years ago

I would like to be able to perform an uppercase transform on a regex replacement. For example:

check("STUFFabc".replace(re"STUFF([a-z])([a-z]+)", "\u$1$2") == "Abc")

Basically, I want something like described here

nitely commented 4 years ago

It'd be nice, but in the meantime do this:

import regex
import strutils

proc reTitleCase(m: RegexMatch, s: string): string =
  result.add(s[m.group(0)[0]].toUpperAscii)
  result.add(s[m.group(1)[0]])

doAssert "STUFFabc".replace(re"STUFF([a-z])([a-z]+)", reTitleCase) == "Abc"