You are currently browsing the Boolog posts tagged: VC6


How to use VB regex in VC6

1,Open “OLE/COM object viewer” from Tool menu of VC6.
2,Find “Microsoft VBScript Regular Expressions 5.5” under “Type Libraries”.

3, Double click the node to open with “ITypeLib Viewer”.
4, Choose “Save As” and save as “vbscript.IDL”.

5, Run “midl vbscript.idl” in Command Propmt. midl.exe is typically located under “C:\Program Files\Microsoft Visual Studio\VC98\Bin”.

This creates “vbscript.tlb”.
6, Move this file into your VC6’s project directory.

7, By coding #import “vbscript.tlb” in your source file, VB regex will be available.

#include "stdafx.h"
 
#import "vbscript.tlb" no_namespace named_guids
 
_bstr_t vbregReplace(LPCTSTR source,
					 LPCTSTR replace, 
					 LPCTSTR regex,
					 BOOL bGlobal,
					 BOOL bCaseInsensitive)
{
	IRegExpPtr pReg;
	pReg.CreateInstance(CLSID_RegExp);
 
	_bstr_t bstrSource(source);
	_bstr_t bstrReplace(replace);
	_bstr_t bstrRegex(regex);
 
	pReg->put_Global(bGlobal?VARIANT_TRUE:VARIANT_FALSE); 
	pReg->put_IgnoreCase(bCaseInsensitive?VARIANT_TRUE:VARIANT_FALSE); 
	pReg->put_Pattern(bstrRegex); 
 
	_bstr_t bstrRet = pReg->Replace(bstrSource, bstrReplace);
 
	return bstrRet;
}
 
int main(int argc, char* argv[])
{
	CoInitialize(NULL);
	_bstr_t t = vbregReplace(
		"abcdefg",
		"x",
		"[aceg]",
		TRUE,
		TRUE);
 
	puts(t);
	CoUninitialize();
	return 0;
}