65.9K
CodeProject is changing. Read more.
Home

Convenient wrapper of VBScript.RegExp for VC++

starIconstarIconstarIconstarIcon
emptyStarIcon
starIcon

4.50/5 (7 votes)

Mar 1, 2004

CPOL
viewsIcon

51133

downloadIcon

660

Regular Expression for VC++ using VBScript.RegExp

Introduction

This is a wrapper of "VBScript.RegExp" or an example of how to use "RegExp" in VC++. I have implemented some often used methods. You can add any more as you need.

The VBScript.RegExp's performance is better than JScript.RegExp, but VBScript.RegExp did not provide 'lastIndex' property, so I selected VBScript.RegExp in VC++ and implemented 'lastIndex' by myself. I supply a convenient class RegExp, and the style is like JScript.RegExp.

When you use the regexp.h and regexp.cpp, do not forget to call CoInitializeEx or CoInitialize at the beginning of your program. Any suggestion is welcome. After you finish optimizing the code, please send a copy to me.

 CoInitializeEx( NULL, COINIT_MULTITHREADED );

 RegExp re;
 re.IgnoreCase( TRUE );

 re.compile( _T("\w([\d.]*)(\w+)") );
 re.exec( _T("I f3454ind it, f567.56ind it again") );

 cout << re.index << _T(" ~ ") << re.lastIndex << endl;
 cout << (LPCTSTR)re.SubMatch( 0 ) << endl;
 cout << (LPCTSTR)re.SubMatch( 1 ) << endl << endl;