1 项目描述
在文本中查找替换对应的字符串,功能如下:
thisisbefore(self,func) 替换成thisisafter(func,self).
2详细设计
2.1字符串查找替换
- string regexReplace(string sMsg,string sSreach,string sReplace)
- {
- string sRet = "";
- std::regex rPattern(sSreach);//搜索串
- sRet = std::regex_replace(sMsg,rPattern,sReplace);
- return sRet;
- }
2.2self与func交换位置
- string regexSelfChangePos(string sMsg)
- {
- string sFunc = "";
- string sSelf = "";
- std::smatch rPotRet;
- std::regex rPotPattern("[(](\\s*[a-zA-Z]+[0-9]*[a-zA-Z]+[0-9]*[.]*[a-zA-Z]+[0-9]*[a-zA-Z]+[0-9]*\\s*),(\\s*[a-zA-Z]+[0-9]*[a-zA-Z]+[0-9]*[.]*[a-zA-Z]+[0-9]*[a-zA-Z]+[0-9]*\\s*)[)]");
- const std::sregex_token_iterator end;
- for (std::sregex_token_iterator itPot(sMsg.begin(),sMsg.end(),rPotPattern); itPot != end; ++itPot)
- {
- std::string sPot = *itPot;
- if (std::regex_search(sPot,rPotRet,rPotPattern))
- {
- sFunc = rPotRet[1].str();
- sSelf = rPotRet[2].str();
- }
- }
- sFunc = regexDelBlank(sFunc);
- sSelf = regexDelBlank(sSelf);
- string tmp = sSelf + "Tmp";
- sMsg = regexReplace(sMsg,sFunc,tmp);
- sMsg = regexReplace(sMsg,sSelf,sFunc);
- sMsg = regexReplace(sMsg,sFunc + "Tmp",sSelf);
- return sMsg;
- }
2.3文件读写操作
- int fileTest()
- {
- char* old_locale = _strdup(setlocale(LC_CTYPE,NULL));
- setlocale(LC_CTYPE,"chs");
- CString StrFileName("d:\\fileTest.lua");
- CStdioFile TempFile,File;
- if (!File.Open(StrFileName,CFile::modeRead))
- return -1;
- CString StrTempFileName = File.GetFileTitle() + CString(".tmp");
- if (!TempFile.Open(StrTempFileName,CFile::modeCreate | CFile::modeReadWrite))
- return -1;
- CString Str;
- while (File.ReadString(Str))
- {
- string sMsg = (CStringA)Str;
- sMsg = clickDeal(sMsg);
- CString strNew(sMsg.c_str());
- TempFile.WriteString(strNew + CString("\n"));
- }
- File.Close();
- TempFile.Close();
- CFile::Remove(StrFileName);
- CFile::Rename(StrTempFileName,StrFileName);
- printf("successful ");
- setlocale(LC_CTYPE,old_locale); //还原语言区域的设置
- free(old_locale);//还原区域设定
- return 0;
- }
全部源代码
- // Regex2.cpp : 定义控制台应用程序的入口点。
- //
- #include "stdafx.h"
- #include "Regex2.h"
- #include "stdafx.h"
- #include <regex>
- #include <iostream>
- #include <string>
- #include "Windows.h"
- #include "Windef.h"
- #include "atltypes.h"
- #include <atlconv.h>
- #include <locale.h>
- // 唯一的应用程序对象
- CWinApp theApp;
- using namespace std;
- #ifdef _DEBUG
- #define new DEBUG_NEW
- #endif
- // 唯一的应用程序对象
- using namespace std;
- //替换
- string regexReplace(string sMsg,sReplace);
- return sRet;
- }
- string regexDelBlank(string sMsg)
- {
- string sSreach = "\\s*";
- std::regex rPattern(sSreach);//搜索串
- sMsg = std::regex_replace(sMsg,"");
- return sMsg;
- }
- string regexSelfChangePos(string sMsg)
- {
- string sFunc = "";
- string sSelf = "";
- std::smatch rPotRet;//([0-9]+) ([a-zA-Z]+)
- std::regex rPotPattern("[(](\\s*[a-zA-Z]+[0-9]*[a-zA-Z]+[0-9]*[.]*[a-zA-Z]+[0-9]*[a-zA-Z]+[0-9]*\\s*),sSelf);
- return sMsg;
- }
- string clickDeal(string sMsg)
- {
- string sOut = regexReplace(sMsg,"thisisbefore","thisisafter");
- sMsg = regexSelfChangePos(sOut);
- return sMsg;
- }
- int fileTest()
- {
- char* old_locale = _strdup(setlocale(LC_CTYPE,old_locale); //还原语言区域的设置
- free(old_locale);//还原区域设定
- return 0;
- }
- int _tmain(int argc,TCHAR* argv[],TCHAR* envp[])
- {
- int nRetCode = 0;
- HMODULE hModule = ::GetModuleHandle(NULL);
- if (hModule != NULL)
- {
- // 初始化 MFC 并在失败时显示错误
- if (!AfxWinInit(hModule,NULL,::GetCommandLine(),0))
- {
- // TODO: 更改错误代码以符合您的需要
- _tprintf(_T("错误: MFC 初始化失败\n"));
- nRetCode = 1;
- }
- else
- {
- // TODO: 在此处为应用程序的行为编写代码。
- fileTest();
- }
- }
- else
- {
- // TODO: 更改错误代码以符合您的需要
- _tprintf(_T("错误: GetModuleHandle 失败\n"));
- nRetCode = 1;
- }
- getchar();
- return nRetCode;
- }