Terbium-135
Regex replace: Replace only (first) group of a match
Given this (TeX) string (with a newline at the end):
\fill[cardbackground, rounded corners=2pt] (0,0) rectangle (\cardwidth,\cardheight);
I am able to match with this regex:
~r/.*(cardbackground).*;\n/
How do I replace only the first group match (which is cardbackground in this case) using something like:
Regex.replace(~r/.*(cardbackground).*;\n/, source, "{RGB}{245,245,245}")
Looking at Regex — Elixir v1.18.4 is of not much help (to me).
What do I miss?
Marked As Solved
al2o3cr
If you only want to replace cardbackground, only match that:
Regex.replace(~r/cardbackground/, source, ...)
If you want to look for a pattern but not include it in the match, you can use a positive lookahead assertion:
Regex.replace(~r/cardbackground(?=.*;\n)/, source, ...)
This still only matches cardbackground alone, so that’s the only part that gets replaced. The assertion looks for ;\n at the end of the line.








