微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

正则表达式查找替换

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

原文地址:https://www.jb51.cc/regex/359098.html

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐