vs2010-mfc-怎么用正则regex?

feelingseas 2014-09-18 09:18:48
开始以为vs没有正则头文件,弄了好久也没弄成boost里的regex。。。最后发现说vs2010之后是有regex的。。。
没那么多时间了(其实也是不太会c++模版技术),所以偷个懒,大家帮我一下吧。
我的需求是 找到一个html页面的标签:
比如
<div class="header">
我要把header替换成我的一个值。。
要在mfc下的,不知道该怎么弄。。。

///////////////////////////////
#include <regex>
CString str = "<div class=\"header\">";
CString strRegex = "class\"(.*)\"";

然后呢??谢谢了。。
(用regex_replace?but how ??)
regex_replace函数定义:

std::tr1::regex_replace(_OutIt,_BidIt,_BidIt,const std::tr1::basic_regex<_Elem,_RxTraits> &,const std::basic_string<_Elem> &,std::tr1::regex_constants::match_flag_type)
...全文
561 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
feelingseas 2014-09-18
  • 打赏
  • 举报
回复 1
我的代码,mfc下的!我是参看了微软msdn弄的!
	std::regex rx("class=\"(.*)\"");
	//CString rx("class\"(.*)\"");
	std::string fmt("class=\"new\"");
	//CString fmt("class\"(.*)\"");
	std::regex_constants::match_flag_type fonly = 
		std::regex_constants::format_first_only;
	std::string str("<div class=\"header\">"); 
	str = std::regex_replace(str, rx, fmt);
	MessageBox(str.c_str());
feelingseas 2014-09-18
  • 打赏
  • 举报
回复
引用 2 楼 removefile 的回复:
// text to transform std::string text = "This is a element and this a unique ID."; // regular expression with two capture groups const std::regex pattern("(\\ba (a|e|i|u|o))+"); // the pattern for the transformation, using the second // capture group std::string replace = "an $2"; std::string newtext = std::regex_replace(text, pattern, replace); std::cout << newtext << std::endl; std::cout << std::endl; return 0; } 运行结果: 还是来说明一下,这里主要使用了regex_replace(text, pattern, replace),意思是将text的内容按照pattern进行匹配,匹配成功的使用replace串进行替换,并将替换后的结果作为函数值返回。需要注意的是std::string replace = "an $2"; 这里‘$2’表示模式串的第二个子表达式, 也就是以a,e,i,o,u开头的单词。 C++的正则使用时一定要注意使用转义字符 \ , 正则表达式的几个元字符 [ ] \ ^ $ .| ? * + ( ) 都要用转义字符
//感谢您 这么及时的的回答!!!不过我也刚自己弄出来了,呵呵。分都给你吧!!
removefile 2014-09-18
  • 打赏
  • 举报
回复
// text to transform
std::string text = "This is a element and this a unique ID.";

// regular expression with two capture groups
const std::regex pattern("(\\ba (a|e|i|u|o))+");

// the pattern for the transformation, using the second
// capture group
std::string replace = "an $2";

std::string newtext = std::regex_replace(text, pattern, replace);

std::cout << newtext << std::endl;
std::cout << std::endl;
return 0;
}
运行结果:

还是来说明一下,这里主要使用了regex_replace(text, pattern, replace),意思是将text的内容按照pattern进行匹配,匹配成功的使用replace串进行替换,并将替换后的结果作为函数值返回。需要注意的是std::string replace = "an $2"; 这里‘$2’表示模式串的第二个子表达式,
也就是以a,e,i,o,u开头的单词。

C++的正则使用时一定要注意使用转义字符 \ , 正则表达式的几个元字符 [ ] \ ^ $ .| ? * + ( ) 都要用转义字符
feelingseas 2014-09-18
  • 打赏
  • 举报
回复
原定义:
	// TEMPLATE FUNCTION regex_replace
template<class _OutIt,
	class _BidIt,
	class _RxTraits,
	class _Elem> inline
	_OutIt _Regex_replace(_OutIt _Result,
		_BidIt _First, _BidIt _Last,
		const basic_regex<_Elem, _RxTraits>& _Re,
		const _STD basic_string<_Elem>& _Fmt,
		regex_constants::match_flag_type _Flgs)
	{	// search and replace
	match_results<_BidIt> _Matches;
	_BidIt _Pos = _First;
	regex_constants::match_flag_type _Flags = _Flgs;
	regex_constants::match_flag_type _Not_null =
		(regex_constants::match_flag_type)0;

	while (regex_search(_Pos, _Last, _Matches, _Re, _Flags | _Not_null))
		{	// replace at each match
		if (!(_Flgs & regex_constants::format_no_copy))
			_Result = _STD _Copy_impl(_Matches.prefix().first,
				_Matches.prefix().second, _Result);
		_Result = _Matches._Format(_Result, _Fmt, _Flags);

		_Pos = _Matches[0].second;
		if (_Pos == _Last
			|| _Flgs & regex_constants::format_first_only)
			break;

		if (_Matches[0].first == _Matches[0].second)
			_Not_null = regex_constants::_Match_not_null;
		else
			{	// non-null match, recognize earlier text
			_Not_null = (regex_constants::match_flag_type)0;
			_Flags |= regex_constants::match_prev_avail;
			}
		}
	return (_Flgs & regex_constants::format_no_copy
		? _Result : _STD _Copy_impl(_Pos, _Last, _Result));
	}

16,470

社区成员

发帖
与我相关
我的任务
社区描述
VC/MFC相关问题讨论
社区管理员
  • 基础类社区
  • Web++
  • encoderlee
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

        VC/MFC社区版块或许是CSDN最“古老”的版块了,记忆之中,与CSDN的年龄几乎差不多。随着时间的推移,MFC技术渐渐的偏离了开发主流,若干年之后的今天,当我们面对着微软的这个经典之笔,内心充满着敬意,那些曾经的记忆,可以说代表着二十年前曾经的辉煌……
        向经典致敬,或许是老一代程序员内心里面难以释怀的感受。互联网大行其道的今天,我们期待着MFC技术能够恢复其曾经的辉煌,或许这个期待会永远成为一种“梦想”,或许一切皆有可能……
        我们希望这个版块可以很好的适配Web时代,期待更好的互联网技术能够使得MFC技术框架得以重现活力,……

试试用AI创作助手写篇文章吧