头文件:
#pragma once
namespace VBScript_RegExp_55
{
class CMatch2 : protected COleDispatchDriver
{
friend class CMatchCollection2;
CMatch2(LPDISPATCH pDispatch);
public:
CString GetValue();
LONG GetFirstIndex();
LONG GetLength();
};
class CMatchCollection2 : protected COleDispatchDriver
{
friend class CRegExp2;
CMatchCollection2(LPDISPATCH pDispatch);
public:
CMatch2 GetItem(LONG index);
LONG GetCount();
};
// CRegExp2 包装类
class CRegExp2 : public COleDispatchDriver
{
public:
CRegExp2(); // 调用 COleDispatchDriver 默认构造函数
~CRegExp2();
BOOL CreateByReg();
private:
CRegExp2(const CRegExp2 &rhs);
CRegExp2 &operator=(const CRegExp2 &rhs);
// IRegExp2 方法
public:
CString GetPattern();
void SetPattern(LPCTSTR newValue);
BOOL IsIgnoreCase();
void SetIgnoreCase(BOOL newValue = TRUE);
BOOL IsGlobal();
void SetGlobal(BOOL newValue = TRUE);
BOOL IsMultiline();
void SetMultiline(BOOL newValue = TRUE);
CMatchCollection2 Execute(LPCTSTR sourceString);
BOOL Test(LPCTSTR sourceString);
CString Replace(LPCTSTR sourceString, LPCTSTR replaceString);
};
}
实现文件:
#include "stdafx.h"
#include "RegExp.h"
using namespace VBScript_RegExp_55;
//******************************************************************************
CMatch2::CMatch2(LPDISPATCH pDispatch)
: COleDispatchDriver(pDispatch)
{
}
CString CMatch2::GetValue()
{
CString result;
InvokeHelper(0x0, DISPATCH_PROPERTYGET, VT_BSTR, (void*)&result, NULL);
return result;
}
LONG CMatch2::GetFirstIndex()
{
LONG result;
InvokeHelper(0x2711, DISPATCH_PROPERTYGET, VT_I4, (void*)&result, NULL);
return result;
}
LONG CMatch2::GetLength()
{
LONG result;
InvokeHelper(0x2712, DISPATCH_PROPERTYGET, VT_I4, (void*)&result, NULL);
return result;
}
//******************************************************************************
CMatchCollection2::CMatchCollection2(LPDISPATCH pDispatch)
: COleDispatchDriver(pDispatch)
{
TRACE("CMatchCollection2::CMatchCollection2\r\n");
}
CMatch2 CMatchCollection2::GetItem(LONG index)
{
LPDISPATCH result = NULL;
static BYTE parms[] = VTS_I4 ;
InvokeHelper(0x0, DISPATCH_PROPERTYGET, VT_DISPATCH, (void*)&result, parms, index);
return CMatch2(result);
}
LONG CMatchCollection2::GetCount()
{
LONG result = 0;
InvokeHelper(0x1, DISPATCH_PROPERTYGET, VT_I4, (void*)&result, NULL);
return result;
}
//******************************************************************************
CRegExp2::CRegExp2()
{
}
CRegExp2::~CRegExp2()
{
ReleaseDispatch();
}
BOOL CRegExp2::CreateByReg()
{
return CreateDispatch(_T("VBScript.RegExp"));
}
CString CRegExp2::GetPattern()
{
CString result;
InvokeHelper(0x2711, DISPATCH_PROPERTYGET, VT_BSTR, (void*)&result, NULL);
return result;
}
void CRegExp2::SetPattern(LPCTSTR newValue)
{
static BYTE parms[] = VTS_BSTR ;
InvokeHelper(0x2711, DISPATCH_PROPERTYPUT, VT_EMPTY, NULL, parms, newValue);
}
BOOL CRegExp2::IsIgnoreCase()
{
BOOL result = FALSE;
InvokeHelper(0x2712, DISPATCH_PROPERTYGET, VT_BOOL, (void*)&result, NULL);
return result;
}
void CRegExp2::SetIgnoreCase(BOOL newValue)
{
static BYTE parms[] = VTS_BOOL ;
InvokeHelper(0x2712, DISPATCH_PROPERTYPUT, VT_EMPTY, NULL, parms, newValue);
}
BOOL CRegExp2::IsGlobal()
{
BOOL result = FALSE;
InvokeHelper(0x2713, DISPATCH_PROPERTYGET, VT_BOOL, (void*)&result, NULL);
return result;
}
void CRegExp2::SetGlobal(BOOL newValue)
{
static BYTE parms[] = VTS_BOOL ;
InvokeHelper(0x2713, DISPATCH_PROPERTYPUT, VT_EMPTY, NULL, parms, newValue);
}
BOOL CRegExp2::IsMultiline()
{
BOOL result = FALSE;
InvokeHelper(0x2717, DISPATCH_PROPERTYGET, VT_BOOL, (void*)&result, NULL);
return result;
}
void CRegExp2::SetMultiline(BOOL newValue)
{
static BYTE parms[] = VTS_BOOL ;
InvokeHelper(0x2717, DISPATCH_PROPERTYPUT, VT_EMPTY, NULL, parms, newValue);
}
CMatchCollection2 CRegExp2::Execute(LPCTSTR sourceString)
{
LPDISPATCH result = NULL;
static BYTE parms[] = VTS_BSTR ;
InvokeHelper(0x2714, DISPATCH_METHOD, VT_DISPATCH, (void*)&result, parms, sourceString);
return CMatchCollection2(result);
}
BOOL CRegExp2::Test(LPCTSTR sourceString)
{
BOOL result = FALSE;
static BYTE parms[] = VTS_BSTR ;
InvokeHelper(0x2715, DISPATCH_METHOD, VT_BOOL, (void*)&result, parms, sourceString);
return result;
}
CString CRegExp2::Replace(LPCTSTR sourceString, LPCTSTR replaceString)
{
CString result;
static BYTE parms[] = VTS_BSTR VTS_BSTR ;
InvokeHelper(0x2716, DISPATCH_METHOD, VT_BSTR, (void*)&result, parms, sourceString, replaceString);
return result;
}
具体使用:
记得 CoInitialize(NULL);
VBScript_RegExp_55::CRegExp2 RegExp;
RegExp.CreateByReg();
RegExp.SetPattern("((2[0-4]\\d|25[0-5]|[01]?\\d\\d?)\\.){3}(2[0-4]\\d|25[0-5]|[01]?\\d\\d?)");//IP地址
VBScript_RegExp_55::CMatchCollection2 myc = RegExp.Execute("sdffdgfd192.168.101.23w2r4tfghtyj");
AfxMessageBox(myc.GetItem(0).GetValue());
posted on 2011-01-09 14:04
张志松 阅读(732)
评论(0) 编辑 收藏 引用 所属分类:
VC/MFC