VC中的正则表达式使用

简介: 对于很多应用,例如文件查询和名称匹配,在html文件中查找匹配的超链接等等 正则表达式自然是最好的解决方法. 不过VC里面竟然不如VBS对于Regular Expression那样有支持, 除非升级到VC.NET(使用拖管C++就可以使用).
对于很多应用,例如文件查询和名称匹配,在html文件中查找匹配的超链接等等 正则表达式自然是最好的解决方法. 不过VC里面竟然不如VBS对于Regular Expression那样有支持, 除非升级到VC.NET(使用拖管C++就可以使用).
    其实VBS也是使用了微软自己REGEX的一个COM, 称为 "Microsoft VBScript Regular Expression 5.5" 在OleViewer可以查找到相关信息. 下面是那篇文章的使用例子
None.gif#import "RegExp.tlb" no_namespace
None.gif   dot.gif
ExpandedBlockStart.gif   try  {
InBlock.gif    static IRegExpPtr regExp( __uuidof(RegExp) );
InBlock.gif    regExp->Pattern = _bstr_t(lpszPattern);
InBlock.gif  
InBlock.gif    HWND hWndCtrl = pDX->PrepareEditCtrl(nIDC);
InBlock.gif    if (pDX->m_bSaveAndValidate)
ExpandedSubBlockStart.gif    {
InBlock.gif        int nLen = ::GetWindowTextLength(hWndCtrl);
InBlock.gif        ::GetWindowText(hWndCtrl, value.GetBufferSetLength(nLen),nLen+1);
InBlock.gif        value.ReleaseBuffer();
InBlock.gif  
InBlock.gif        //now we verify it
InBlock.gif
        if ( regExp->Test( (LPCTSTR)value) )
ExpandedSubBlockStart.gif        {
InBlock.gif            IMatchCollectionPtr matches=regExp->Execute((LPCTSTR)value);
InBlock.gif            if ( matches->Count== 1)
ExpandedSubBlockStart.gif            {
InBlock.gif                IMatchPtr match = matches->Item[0];
InBlock.gif                if ( match->FirstIndex==0 && match->Length == value.GetLength() )
ExpandedSubBlockStart.gif                {
InBlock.gif                  return;
ExpandedSubBlockEnd.gif                }

ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

InBlock.gif        CString strMsg = CString("The input does not exactly have the pattern ") + lpszPattern;
InBlock.gif        pDX->m_pDlgWnd->MessageBox(strMsg);
InBlock.gif        pDX->PrepareEditCtrl(nIDC);
InBlock.gif        pDX->Fail();
ExpandedSubBlockEnd.gif    }

InBlock.gif    else
ExpandedSubBlockStart.gif    {
ExpandedSubBlockEnd.gif    }

ExpandedBlockEnd.gif  }

None.gif   catch (_com_error& e)
ExpandedBlockStart.gif   {
InBlock.gif      AfxMessageBox( e.ErrorMessage() );
ExpandedBlockEnd.gif  }
目录
相关文章
|
11月前
|
数据采集 Python
python|正则表达式-3:基本特殊字符含义
python|正则表达式-3:基本特殊字符含义
75 0
|
IDE 编译器 开发工具
MFC的宏_T()和_L()的区别,以及VS2017正则表达式批量替换_T()
MFC的宏_T()和_L()的区别,以及VS2017正则表达式批量替换_T()
386 0
MFC的宏_T()和_L()的区别,以及VS2017正则表达式批量替换_T()
|
Python
python 正则表达式匹配中文
版权声明:本文为 testcs_dn(微wx笑) 原创文章,非商用自由转载-保持署名-注明出处,谢谢。 https://blog.csdn.net/testcs_dn/article/details/79293722 ...
975 0
|
C++
Visual Studio查找中文的正则表达式
原文: Visual Studio查找中文的正则表达式 经常有这样的需求:项目代码中有一些输出信息是中文写的,不过现在要做国际化,代码""中写的中文都要改成英文。这样就需要将代码中包含中文的字符串都找出来。
1076 0
|
程序员 API 开发工具