簡體   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