score:1

Accepted answer

you can't do that only with regex and a simple search & replace because you'll have to do maths.

here's one of the algorithm that does what you want to do:

int position = 70;
list<string> output = new list<string>();
foreach (string line in file.readalllines(@"c:\users\me\desktop\test.txt"))
{
    // the line contains comments and code
    if (line.contains(@"//") && !regex.ismatch(line, @"^\s*//"))
    {
        var matchcollection = regex.matches(line, @"(?<code>.*;)\s*(?<comment>\/\/.*)");
        string code = matchcollection[0].groups["code"].value;
        string comment = matchcollection[0].groups["comment"].value;

        output.add(code.padright(position) + comment);
    }
    else
    {
        output.add(line);
    }
}

file.writealllines(@"c:\users\me\desktop\out.txt", output);

it's written in , since it's pretty close to pseudo code you'll be able to port it easily (at least i hope). here are the main nontrivial parts (do not hesitate if you need more explanations):

  • regex.ismatch(line, @"^\s*//"): find the lines that are just comments
  • (?<code>.*;): a named capture group
  • code.padright(position): padrigth add white space to the right of the string up to position

given this input:

//--------------------------------------------------------------------------!
// unveil left to right                                                     !
//--------------------------------------------------------------------------!

static void punveill(ulong frame, ulong field)
{
    frame* thisframe = &frames[frame] ;                             // make code more readable
    field* thisfield = &fields[frames[frame].fields[field]] ;               // make code more readable

the program outputs:

//--------------------------------------------------------------------------!
// unveil left to right                                                     !
//--------------------------------------------------------------------------!

static void punveill(ulong frame, ulong field)
{
    frame* thisframe = &frames[frame] ;                               // make code more readable
    field* thisfield = &fields[frames[frame].fields[field]] ;         // make code more readable

Related Query

More Query from same tag