Youssef Shoaib [MOD]
03/30/2025, 2:04 PMRegex.replaceEntire
? I'm aware of matchEntire
, but I want to be able to use references to capture groups in the replacement string. Is there some easy way to use a MatchResult
for constructing a string where it aptly resolves capture group references?
EDIT: solved! Just use ^...$
lol! Question is, is there a way to take a regex and turn it into that form programmatically?Joffrey
03/30/2025, 2:25 PMinline fun CharSequence.replace(
regex: Regex,
noinline transform: (MatchResult) -> CharSequence
): String
Youssef Shoaib [MOD]
03/30/2025, 2:26 PMMatchResult
and construct a replacement string easily by replacing the capture group referencesJoffrey
03/30/2025, 2:27 PMYoussef Shoaib [MOD]
03/30/2025, 2:29 PM"$1$2${foo}"
) to replace those references, while also supporting escapes thru backslashes etc. Seems to me like there should be a built-in solutionJoffrey
03/30/2025, 3:16 PMJoffrey
03/30/2025, 3:19 PMYoussef Shoaib [MOD]
03/30/2025, 3:20 PMreplaceEntire
function, so my options are to either modify the regex, or to use matchEntire
and manually process the replacement string to deal with capture group references. I think I'll go with modifying the regex for now! But I'm still curious to hear if any other options existJoffrey
03/30/2025, 3:22 PMreplace
with a regex that matches the start and end of input. You could also construct a new regex that enforces this programmatically, but that might require some carefulness with whether the original ends with a backslash or something.Vampire
03/30/2025, 4:01 PMJust use ^...$ lol! Question is, is there a way to take a regex and turn it into that form programmatically?
Use a non-capturing group.
^(?:originalRegexHere)$
Vampire
03/30/2025, 4:04 PMVampire
03/30/2025, 4:05 PMephemient
03/30/2025, 4:07 PMVampire
03/30/2025, 4:34 PM