I have been out of touch with programming for a while. I have some strings I want to format. They look like this:
,SUM(CASE WHEN [Level] IN('4') AND Program = 1 THEN 1 ELSE 0 END) as CNT1
,SUM(CASE WHEN [Level] IN('3') AND Program = 1 THEN 1 ELSE 0 END) as CNT2
,SUM(CASE WHEN [Level] IN('2') AND Program = 1 THEN 1 ELSE 0 END) as CNT3
,SUM(CASE WHEN [Level] IN('1') AND Program = 1 THEN 1 ELSE 0 END) as CNT4
,SUM(CASE WHEN [Level] IN('0') AND Program = 1 THEN 1 ELSE 0 END) as CNT5
I want to take this and change it to
,CNT1 = SUM(CASE WHEN [Level] IN('4') AND Program = 1 THEN 1 ELSE 0 END)
,CNT2 = SUM(CASE WHEN [Level] IN('3') AND Program = 1 THEN 1 ELSE 0 END)
,CNT3 = SUM(CASE WHEN [Level] IN('2') AND Program = 1 THEN 1 ELSE 0 END)
,CNT4 = SUM(CASE WHEN [Level] IN('1') AND Program = 1 THEN 1 ELSE 0 END)
,CNT5 = SUM(CASE WHEN [Level] IN('0') AND Program = 1 THEN 1 ELSE 0 END)
right now I have a multiline textbox that I paste the strings in. I want to click a button and have it formatted into a second textbox.
This is what I’ve tried so far. but I am not sure what string functions to use format it the way i want.
List<String> Items = new List<string>();
string frTxt;
foreach (string lne in txtM.Lines)
{
frTxt = "";
frTxt = lne;
}
where txtM is the multiline textbox. If I do Console.WriteLine(lne); in the loop, it loops through successfully.
Assuming the format is always
This the following will transform it to:
Explanation of regex:
As a result of combining the greedy and non-greedy matching, we force the ” as …” to be the last occurrence (so if the string happens to contain ” as ” somewhere in it, that won’t matter). By adding the “$” we ensure that the second capture takes everything to the end of the line (otherwise as it’s a non-greedy match it would match just the first character after ” as “, which isn’t what we want).
The replacement simply constructs the new string using the two groups captured by the regex, adding the “=”. We also manually add back the “,” at the beginning because our regex matches on it and so it will be replaced by the replacement string. (This can be overcome but would probably make the regex more complex).