繁体   English   中英

使用正则表达式替换Notepad ++

[英]Replacing in Notepad++ using regular expression

我有一个cpp文件,其中包含一个全局数组定义,但是不幸的是,编写定义的那个人没有使用浮点值的文字(1.0f instad为1.0),所以我决定使用notepad ++来实现。 数组定义如下(它非常大,如10000行代码):

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,
...

有人可以使用记事本++帮助我将0、0.984314、1.1.0f行替换为0、0.984314f,1.0f,1.0f吗?

有些人在遇到问题时会认为“我知道,我会使用正则表达式”。 现在他们有两个问题。

  • JWZ

您无需在这里使用正则表达式,只需两个查找/替换操作。

  1. 将数组定义复制/粘贴到新文档中
  2. 查找/替换所有逗号f,
  3. 查找/替换所有换行符(可以是\\r\\r\\n\\n ,具体取决于所使用的平台和其他编辑器-您必须检查一下自己),后跟0f,后跟换行符,然后为0, 由于特殊字符,您必须为此使用“扩展”选项
  4. 将数组定义复制/粘贴回原始文件

我分两步来做:

第一步

搜索, *1,

替换, 1.0, (带或不带“ f”)

第二步

搜索(\\d+\\.\\d+)(?!f)

替换$1f

我还用一小段代码解决了这个问题:

#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";
    }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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