views:

50

answers:

2

Input:

X(P)~AK,X(MV)~AK

Expected Output:

P(curr=AK),MV(curr=AK)

Using C#3.0

I solved by using string functions(split,then appending etc.)

Looking for more niche solution(like regular expression)

Thanks

+1  A: 

A possible solution using regex. It's not the cleanest, but you could go from here.

string process(string s)
{
    string ret = "";
    System.Text.RegularExpressions.Regex r = new Regex(@"X\(([A-Z]+)\)~([^,]+)\,?");
    bool first = true;
    foreach (Match m in r.Matches(s))
    {
        ret += (first ? "" : ",") + m.Groups[1] + "(curr=" + m.Groups[2] + ")";
        first = false;
    }
    return ret;
}
Zaki
+8  A: 
var output = Regex.Replace(input, @"X\(([A-Z]+)\)~([A-Z]+)", "$1(curr=$2)");

This will replace all occurrences of X(something1)~something2 with something1(curr=something2). All "something"s are assumed to be sequences of uppercase characters.

Heinzi