Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

find and replace in eclipse "go to nth character position"

Tags:

regex

eclipse

I have some large c source files and I would like to align, all end of line comments to the nth character. take the following:``

//--------------------------------------------------------------------------!
// 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 comment above the function is roughly 60-70 characters (we'll say 70). So I want both the end of line comments to start from the 71th character. Like so

//--------------------------------------------------------------------------!
// 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

To round up, I think I have the find sorted or can manage it, but I need a replacement argument to start the comment // from the nth position in the line. Is the replace part possible ?

like image 482
10SecTom Avatar asked Dec 08 '25 05:12

10SecTom


1 Answers

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 c#, 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
like image 126
Thomas Ayoub Avatar answered Dec 11 '25 02:12

Thomas Ayoub



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!