牵着老婆满街逛

严以律己,宽以待人. 三思而后行.
GMail/GTalk: yanglinbo#google.com;
MSN/Email: tx7do#yahoo.com.cn;
QQ: 3 0 3 3 9 6 9 2 0 .

VC中的正则表达式使用

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

            }

        }

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

    
else
    
{
    }

  }

  
catch (_com_error& e)
  
{
      AfxMessageBox( e.ErrorMessage() );
  }

  

posted on 2006-04-14 20:51 杨粼波 阅读(1063) 评论(0)  编辑 收藏 引用 所属分类: 文章收藏


只有注册用户登录后才能发表评论。
网站导航: 博客园   IT新闻   BlogJava   知识库   博问   管理