前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >正则表达式编程实例

正则表达式编程实例

作者头像
立羽
发布2023-08-24 13:07:26
1270
发布2023-08-24 13:07:26
举报
文章被收录于专栏:Unity3d程序开发

1.使用c++的正则表达式替换对应内容

代码语言:javascript
复制
std::string sKey = it->first;
		std::string sPattern = "(<)(/)?(" + sKey + ")(>)";
		std::regex rPattern(sPattern);

		std::string sReplace = "$1$2" + it->second + "$4";
		sMsg = std::regex_replace(sMsg, rPattern, sReplace);

sKey为要查找的关键词。sPattern为关键词加上正则格式后的字符串, "(<)(/)?(" + sKey + ")(>)",第一个()中表示有一个"<",第二个()后的?表示在<后是否存在?。整体意思为查"<heros1>", "</heros1>"这样的字符串。  sReplace为匹配串模式  "

2.找出所有的坐标点

代码语言:javascript
复制
std::smatch rPotRet;
			std::regex rPotPattern("[(]([0-9]+),([0-9]+)[)]");
			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))
				{
					CPoint pot;
					pot.x = atoi(rPotRet[1].str().c_str());
					pot.y = atoi(rPotRet[2].str().c_str());
					vecPot.push_back(pot);
				}
			}

"[(]([0-9]+),([0-9]+)[)]": [(]为必有一个(;[0-9]+表示有若干个0-9的数。整个意思就是查找 "(20,89)" ,“(1,22)”这样的字符串。

源代码

代码语言:javascript
复制
// regex1.cpp : 定义控制台应用程序的入口点。
//

#include "stdafx.h"
#include <regex>
#include <iostream>
#include <string>
#include <vector>
#include <map>
#include "Windows.h" 
#include "Windef.h" 
#include "atltypes.h"

typedef std::map<std::string, std::string> MapColorType;
MapColorType GmapColor;

void mapColorInit()
{
	GmapColor.insert(MapColorType::value_type("heros1", "12FFFGSEVF"));
	GmapColor.insert(MapColorType::value_type("heros2", "22FDGRG7"));
	GmapColor.insert(MapColorType::value_type("location", "24FDGRG7"));
}

std::string regexDeal(std::string sMsg, MapColorType mapColor, std::vector<CPoint>& vecPot)
{
	std::string sRet;
	for (MapColorType::iterator it = mapColor.begin(); it != mapColor.end(); it++)
	{
		std::string sKey = it->first;
		std::string sPattern = "(<)(/)?(" + sKey + ")(>)";
		std::regex rPattern(sPattern);

		std::string sReplace = "$1$2" + it->second + "$4";
		sMsg = std::regex_replace(sMsg, rPattern, sReplace);

		if (sKey == "location")
		{
			std::smatch rPotRet;
			std::regex rPotPattern("[(]([0-9]+),([0-9]+)[)]");
			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))
				{
					CPoint pot;
					pot.x = atoi(rPotRet[1].str().c_str());
					pot.y = atoi(rPotRet[2].str().c_str());
					vecPot.push_back(pot);
				}
			}
		}
	}
	return sMsg;
}

int main()
{
	mapColorInit();

	std::vector<CPoint> vecPot;
	std::string text = "<heros1>sixi</heros1><location>(11,11)</location><location>(22,22)</location>";
	std::string sRet = regexDeal(text, GmapColor, vecPot);
	std::cout <<"Input:" << text << std::endl;
	std::cout << "Out:"<<sRet << std::endl;

	for (std::vector<CPoint>::iterator it = vecPot.begin(); it != vecPot.end(); it++)
	{
		std::cout << it->x << std::endl;
		std::cout << it->y << std::endl;
	}
	return 0;
}

运行效果

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2015-08-22,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档