正则表达式查找替换

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.2selffunc交换位置

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

相关文章

正则替换html代码中img标签的src值在开发富文本信息在移动端...
正则表达式
AWK是一种处理文本文件的语言,是一个强大的文件分析工具。它...
正则表达式是特殊的字符序列,利用事先定义好的特定字符以及...
Python界一名小学生,热心分享编程学习。
收集整理每周优质开发者内容,包括、、等方面。每周五定期发...