【需求】
从一段文字中匹配出email,将email与其他文字切分开,并标记为超链接。
【分析】
可以使用正则表达式来实现,boost::regex_search可以实现将一段文字的匹配字串列出,通过循环调用达到遍历所有文字。
【实现】
1, VC6.0可以使用boost的Regex,由于Boost从1.34_1后就不支持VC6.0,故采用boost_1.34_1。
2, 添加boost到工程
2.1,可参照此网页http://blog.csdn.net/yysdsyl/article/details/1781546
error: 'memset' : ambiguous call to overloaded function.
编译的时候如果碰到上面的错误,请参考网站做修改:http://lists.boost.org/boost-users/2008/12/43116.php
网页上的方法是对所有工程适用,如果想对某个固定的工程适用,需要做以下几处修改:
-
将boost_1_34_1文件夹放在工程目录下,并将编译好的Regex lib文件夹boostRegexLib放在boost_1_34_1目录下。
-
Settings->C++->Category->Preprocessor->Additional includedirectories:添加 boost_1_34_1
-
Settings->Link->Category->Input->Additional librarypath:添加 boost_1_34_1\boostRegexLib
-
链接的时候会提示错误,LINK: fatal error LNK1104: cannot open file"libboost_regex-vc6-mt-1_34_1.lib",此时应该修改”libboost_regex-vc6-mt-1_34.lib”为”libboost_regex-vc6-mt-1_34_1.lib”
3, 使用boost的Regex
经查看,boost::regex_search最符合预期,它会将一段文字的匹配的字串列出,可以通过循环调用达到遍历所有文字。
可以将boost::regex_search的匹配结果(email在原字串中的起始位置和结束位置)依次装入队列中,取得所有email的遍历结果后,遍历字串,
每次boost::regex_searc匹配的结果会
调用boost::regex_search
//save matchresult
boost::cmatchmatch;
// email regex:"\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*"
boost::regexreg("(\\w+([-+.]\\w+)*@\\w+([-.]\\w+)*\\.\\w+([-.]\\w+)*)");
while(boost::regex_search(pStr, match, reg))
{
…
pStr = match[0].second;
}