现在的位置: 首页 > 综合 > 正文

MFC 中正则表达式的使用,找不到atlrx.h解决办法

2014年05月09日 ⁄ 综合 ⁄ 共 1634字 ⁄ 字号 评论关闭

正好做的程序需要从读取网页中的部分信息,便想到了正则。

vs2008.。。

MSDN下

template < class CharTraits = CAtlRECharTraits > class CAtlRegExp

有个例子程序

  1. // catlregexp_class.cpp  
  2. #include <afx.h>  
  3. #include <atlrx.h>  
  4.   
  5. int main(int argc, char* argv[])  
  6. {  
  7.     CAtlRegExp<> reUrl;  
  8.     // Five match groups: scheme, authority, path, query, fragment  
  9.     REParseError status = reUrl.Parse(  
  10.         "({[^:/?#]+}:)?(//{[^/?#]*})?{[^?#]*}(?{[^#]*})?(#{.*})?" );  
  11.   
  12.     if (REPARSE_ERROR_OK != status)  
  13.     {  
  14.         // Unexpected error.  
  15.         return 0;  
  16.     }  
  17.   
  18.     CAtlREMatchContext<> mcUrl;  
  19.     if (!reUrl.Match(  
  20. "http://search.microsoft.com/us/Search.asp?qu=atl&boolean=ALL#results",  
  21.         &mcUrl))  
  22.     {  
  23.         // Unexpected error.  
  24.         return 0;  
  25.     }  
  26.   
  27.     for (UINT nGroupIndex = 0; nGroupIndex < mcUrl.m_uNumGroups;  
  28.          ++nGroupIndex)  
  29.     {  
  30.         const CAtlREMatchContext<>::RECHAR* szStart = 0;  
  31.         const CAtlREMatchContext<>::RECHAR* szEnd = 0;  
  32.         mcUrl.GetMatch(nGroupIndex, &szStart, &szEnd);  
  33.   
  34.         ptrdiff_t nLength = szEnd - szStart;  
  35.         printf_s("%d: /"%.*s/"/n", nGroupIndex, nLength, szStart);  
  36.     }  
  37.   
  38.     return 0;  
  39. }  

理想输出结果: 
0: "http" 
1: "search.microsoft.com" 
2: "/us/Search.asp" 
3: "qu=atl&boolean=ALL" 
4: "results"

但实际上,如果只做到这里是得不到理想结果的,编译的时候就会得到错误提示,大概的意思是说,这个头文件找不到。

为什么会出现这种情况了,原来是这么一情况 
VS 2008中由于将ALT项目的部分代码剥离出去成为了独立的开源项目,需要用到ALT中正则表达式等功能就需要手动下载。 
我不是第一个遇到这个问题的,所以已经有前人给出了解决方案。

可到http://atlserver.codeplex.com/该网下载所需要的包,将下载到的文件解压到工作目录 
会得到如下图所示的文件,我是解压到D盘符下。 
image

好了,文件都有了,现在就要配置你的项目了

vs2008 ->【工具】->【选项】; 
右边列表选择【项目和解决方案】->【VC++目录】; 
坐上角选择【包含文件】; 
新加入一行,文件路径就是解压文件的路径,如下 
image

这样就ok了,可以使用正则的强大功能了,比自己一个字符一个字符处理强悍n多。

抱歉!评论已关闭.