对于很多应用,例如文件查询和名称匹配,在html文件中查找匹配的超链接等等 正则表达式自然是最好的解决方法. 不过VC里面竟然不如VBS对于Regular Expression那样有支持, 除非升级到VC.NET(使用拖管C++就可以使用).
其实VBS也是使用了微软自己REGEX的一个COM, 称为 "Microsoft VBScript Regular Expression 5.5" 在OleViewer可以查找到相关信息. 下面是那篇文章的使用例子
其实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() );
}
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() );
}