1

is it possible to transform an expression like

{op1 == op2, #} && {op3 > op4, 1, 2} && op5 == op6

to

op1 ==_# op2 && op3 >_1_2 op4 && op5 == op6

So, all occurences after the comma should be placed seperated by an underline after the operator (==, >,<,<=, etc...). opX can be any alphanumerical value.

2
  • 3
    Which language are you using? Commented Aug 6, 2013 at 8:30
  • 2
    This is not easily done with regex. Not in one pass at least. Commented Aug 6, 2013 at 8:40

1 Answer 1

3

After Qtax's comment, I just wrote a solution:

var st = "{op1 == op2, #} && {op3 > op4, 1, 2} && op5 == op6";

var regex = "{.*?}";
for (var match = Regex.Match(st, regex); match.Success; match = Regex.Match(st, regex))
{
    var oldString = match.Value; // {op1 == op2, #} 

    var op = oldString.Split(' ').ToList()[1].Trim(); // == 
    var csv = oldString.Split(',').Select(x => x.Trim()).ToList(); // [0] = "{op1 == op2" [1] = "#}"

    var expression = csv[0].Remove(0,1); // op1 == op2
    csv.RemoveAt(0);

    var extension = "_" + String.Join("_", csv);
    extension = extension.Remove(extension.Length-1); // _#

    var newString = expression.Replace(op, op + extension);

    st = st.Replace(oldString, newString); 
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.