简体   繁体   中英

Replacing in Notepad++ using regular expression

I have a cpp file which contains a global array definition but unfortunately, the guy which was writing the define did not used the literals for float values(1.0f instad of 1.0), so I have decided to do that using notepad++. The array definition is as follows (it is really big, like 10000 lines of code):

const float ifxColorMap::ifxColorScaleCSTFire[] = 
{
0, 1, 1,1.0f,
0, 0.984314, 1,1.0f,
0, 0.968627, 1,1.0f,
0, 0.952941, 1,1.0f,
0, 0.937255, 1,1.0f,
0, 0.921569, 1,1.0f,
0, 0.905882, 1,1.0f,
...

Can anyone please help me with replacing the 0, 0.984314, 1,1.0f, lines with 0, 0.984314f, 1.0f,1.0f using notepad++?

Some people, when confronted with a problem, think "I know, I'll use regular expressions." Now they have two problems.

  • JWZ

You don't need to use a regex here, just two find/replace operations.

  1. Copy/paste the array definition to a new document
  2. Find/replace all commas with f,
  3. Find/replace all newlines (could be \\r , \\r\\n or \\n , depending on platform & other editor(s) used - you'll have to check for yourself) followed by 0f, with a newline followed by 0, . You have to use the "Extended" option for this because of the special characters
  4. Copy/paste your array definition back to the original file

I would do it in two steps:

Step 1 :

Search , *1,

Replace , 1.0, (with or without an "f")

Step 2 :

Search (\\d+\\.\\d+)(?!f)

Replace $1f

I also fixed the problem with a small chunk of code:

#include <iostream>
#include <fstream>
#include <string>

void main()
{
    std::ifstream inFile("in.txt");
    std::ofstream outFile("out.txt");

    char line[401];
    char buffer[401];
    int bufCounter = 0;
    while(!inFile.eof())
    {
        inFile.getline(line, 400);
        size_t len = strlen(line);
        for(size_t i = 0; i < len; i++)
        {
            if(line[i] == ',')
            {
                buffer[bufCounter] = '\0';
                if(buffer[bufCounter-1] != 'f')
                {
                    // has dot inside
                    size_t j = 0;
                    for(; j < bufCounter; j++)
                        if(buffer[j] == '.')
                            break;

                    if(j == bufCounter)
                        outFile << buffer << ".0f, ";
                    else
                        outFile << buffer << "f, ";
                }
                else
                    outFile << buffer << ", ";

                bufCounter = 0;
            }
            else
            {
                if(line[i] == ' ' || line[i] == '\t')
                    ;// Do Nothing
                else
                    buffer[bufCounter++] = line[i];
            }
        }

        outFile << "\n";
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM