其实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() );
}