c# - Replace Multiple References of a pattern with Regex -
i have string in following form
$kl\u#, $as\gehaeuse#, $kl\tol_plus#, $kl\tol_minus#
basically string made of following parts
- $ = delimiter start
- (some text)
- # = delimiter end
- (all of n times)
i replace each of these sections meaningful text. therefore need extract these sections, based on text inside each section , replace section result. resulting string should this:
12v, 0603, +20%, -20%
the commas , else not contained within section stays is, sections replaced meaningful values.
for question: can me regex pattern finds out these sections can replace them?
you need use regex.replace
method , use matchevaluator
delegate decide replacement value should be.
the pattern need can $
except #
, #
. put middle bit in brackets stored separate group in result.
\$([^#]+)#
the full thing can (up correct appropriate replacement logic):
string value = @"$kl\u#, $as\gehaeuse#, $kl\tol_plus#, $kl\tol_minus#"; string result = regex.replace(value, @"\$([^#]+)#", m => { // method takes matching value , needs return correct replacement // m.value e.g. "$kl\u#", m.groups[1].value bit in ()s between $ , # switch (m.groups[1].value) { case @"kl\u": return "12v"; case @"as\gehaeuse": return "0603"; case @"kl\tol_plus": return "+20%"; case @"kl\tol_minus": return "-20%"; default: return m.groups[1].value; } });
Comments
Post a Comment