mikemind mikemind - 17 days ago 6
C# Question

Unable to remove certain characters between values in c#

I am trying to remove characters starting from (and including)

rgm
up to (and including)
;1.
.

Example input string:



Sum ({rgmdaerudsb;1.Total_Value}, {rgmdaerub;1.Major_Value})


Code:



string strEx = "Sum ({rgmdaerudsb;1.Total_Value}, {rgmdaerub;1.Major_Value})";
strEx = strEx.Substring(0, strEx.LastIndexOf("rgm")) +
strEx.Substring(strEx.LastIndexOf(";1.") + 3);


Result:



Sum ({rgmdaerub;1.Total_Value}, {.Major_Value})


Expected result:



Sum ({Total_Value}, {Major_Value})


Note: only
rgm and ;1.
will remain static and characters between them will vary.

Answer

Try this:

string input = "Sum ({rgmdaerudsb;1.Total_Value}, {rgmdaerub;1.Major_Value})";
string result = Regex.Replace(input, @"rgm.*?;1\.", "");