栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > C/C++/C#

C++正则表达式

C/C++/C# 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

C++正则表达式

1、完全匹配字符串

std::regex_match

std::string str = "value";
std::regex reg("<.*>.*");
bool found = std::regex_match(str, reg); //true

std::string str1 = "beginvalueend";
found = std::regex_match(str1, reg);
std::cout << found << std::endl; //false

str能完全匹配所以结果true,str1前后多了begin end所以不能完全匹配结果false

2、查找字符串中是否包含

std::regex_search

std::string str1 = "beginvalueend";
std::regex reg("<.*>.*");
bool found = std::regex_search(str1, reg); //true
3、截取字符串中匹配的部分

std::regex_match

std::string str = "one";
bool found;
std::smatch m;
std::regex reg("<(.*)>(.*)");
found = std::regex_match(str, m, reg);
if (found)
{
	std::cout << m.str(0) << std::endl;
	std::cout << m.str(1) << std::endl;
	std::cout << m.str(2) << std::endl;
    
    //等价于上面的输出
	//for (auto pos = m.begin(); pos != m.end(); ++pos)
	//	std::cout << *pos << std::endl;
}

()括起来的表示分组的概念,1表示指代第一个分组,要求匹配的值必须和第一分组一样。上面的表达式共有两个分组。

m.str(0)表示正则表达式匹配到的完整字符串;

m.str(1)表示取匹配到的第一分组的值

m.str(2)表示取匹配到的第二分组的值

std::regex_search

std::string str1 = "beginvalueend";
bool found;
std::smatch m;
std::regex reg("<(.*)>(.*)");
found = std::regex_search(str1, m, reg);
if (found)
{
    std::cout << m.prefix().str() << std::endl;
	std::cout << m.suffix().str() << std::endl;
	for (auto pos = m.begin(); pos != m.end(); ++pos)
		std::cout << *pos << std::endl;
}

m.prefix().str() 输出的是匹配字符串的前面的字符串

m.suffix().str()输出的是匹配字符串的后面的字符串

4、连续截取字符串

std::regex_search

std::string str = "one";
std::string str1 = "begin firstsecondthird end";
bool found;
std::smatch m;
std::regex reg("<(.*)>(.*)");
found = std::regex_search(str1, m, reg);
if (found)
{
	for (auto pos = m.begin(); pos != m.end(); ++pos)
		std::cout << *pos << std::endl;
}

看起来好像不对劲,直接截取了...,这是greedy(贪婪)风格导致的。

    std::string str1 = "begin firstsecondthird end";
	bool found;
	std::smatch m;
	std::regex reg("<(.*)>([^<]*)"); //调整正则表达式
	auto pos = str1.cbegin();
	auto end = str1.cend();
	while (std::regex_search(pos, end, m, reg))
	{
		//重新设置下一次搜索的起始位置
		pos = m.suffix().first;

		for (auto iter = m.begin(); iter != m.end(); ++iter)
			std::cout << *iter << std::endl;
		std::cout << std::endl;
	}


 使用起来特别麻烦。。。

std::sregex_iterator

    std::string str1 = "begin firstsecondthird end";
	bool found;
	std::smatch m;
	std::regex reg("<(.*)>([^<]*)");
	std::sregex_iterator pos(str1.begin(), str1.end(), reg);
	std::sregex_iterator end;
	for (; pos != end; ++pos)
	{
		std::cout << pos->str() << std::endl;
		std::cout << pos->str(1) << std::endl;
		std::cout << pos->str(2) << std::endl;
		std::cout << std::endl;
	}

std::sregex_token_iterator

    std::string str1 = "begin firstsecondthird end";
	bool found;
	std::smatch m;
	std::regex reg("<(.*)>([^<]*)");
	std::sregex_token_iterator pos(str1.begin(), str1.end(), reg, {0,1,2});
	std::sregex_token_iterator end;
	for (; pos != end; ++pos)
	{
		std::cout << pos->str() << std::endl;
	}

{0,1,2} 0:取和正则匹配的字符串;1:取第一个分组的字符串;2:取第二分组的字符串。

如果只想拿到one two three 就传入2。

    std::string str2 = "one , two ; three :";
	bool found;
	std::smatch m;
	std::regex reg("[ tn]*[,;:][ tn]*");
	std::sregex_token_iterator pos(str2.begin(), str2.end(), reg, -1);
	std::sregex_token_iterator end;
	for (; pos != end; ++pos)
		std::cout << pos->str() << std::endl;

-1表示匹配正则表达式的我不要,我要其它没有匹配到的字符串

5、替换字符串

std::regex_replace

    std::string str2 = "one , two ; three :";
	std::regex reg("[ tn]*[,;:][ tn]*");
	std::string newstr = std::regex_replace(str2, reg, "|");
	std::cout << newstr << std::endl;

把匹配到的字符串替换成‘|’

    std::string str1 = "begin firstsecondthird end";
	std::regex reg("<(.*)>([^<]*)");
	std::string newstr = std::regex_replace(str1, reg, "<$1 value="$2">");
	std::cout << newstr << std::endl;

把匹配到的字符串替换成想要的形式

常用的也就这些,能熟练运用已经能解决很多问题了 大概吧。

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/925572.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号