5,666,979 members and growing! (15,411 online)
Email Password   helpLost your password?
General Programming » String handling » Regular Expressions     Intermediate

Use regular expression in your C++ program

By Sherwood Hu

how to use the Microsoft regular expression object in your C++ program
VC6, C++Windows, NT4, Win2K, Visual Studio, MFC, ATL, Dev

Posted: 15 Oct 2000
Updated: 25 Jan 2001
Views: 113,803
Bookmarked: 27 times
Announcements
Loading...



Search    
Advanced Search
Sitemap
17 votes for this Article.
Popularity: 4.17 Rating: 3.39 out of 5
1 vote, 7.1%
1
3 votes, 21.4%
2
0 votes, 0.0%
3
5 votes, 35.7%
4
5 votes, 35.7%
5

Introduction

Regular expressions provide a convenient way to specify complicated string pattern for search, replace or validate the text input. Since it is very useful, many people wrote their own library. Many libraries I found are buggy and it takes a lot of time to debug source code. However, actually you do not need to search any more, since there is already one built in your computer for free – the regular expression parser written by Microsoft.

As many other utilities, Microsoft provides this functionality using COM interface. It is easy to find this COM server object, named Microsoft VBScript Regular Expressions 5.5, with the tool oleview:

One problem is that there is no type library associated with this DLL. Fortunately it is not a big deal since we have the IDL definition. From OleView, save the IDL definition into a file and use MIDL to compile it, you will get type library. After that, we can use this type library within our C++ program. Suppose we have this file named RegExp.tlb.

Using regular expression

You can find all the documentation in either MSDN or the URL http://msdn.microsoft.com/scripting/default.htm?/scripting/vbscript/doc/vsobjregexp.htm. Though it is for scripting, you can still use them directly with the help of the newly-added keyword #import since Visual C++ 6. Generally you define a pattern, then you can test this pattern against the input string. or exacute to see whether they are any matches.

To demonstrate its usage, we wrote a custome DDX routine to verify the input of a control in a dialog. The function prototype is listed as following:

void WINAPI DDX_RegExp(CDataExchange* pDX, int nIDC, LPCTSTR lpszPattern, CString& value);

If the control input exactly matches the specified pattern(lpszPattern), the validation is passed otherwise an message box will pop up.

#import "RegExp.tlb" no_namespace
...
void AFXAPI DDX_RegExp(CDataExchange* pDX, int nIDC, LPCTSTR lpszPattern, CString& value)
{
   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() );
    }
}

In the code above, we first use Test method to see whether there is a match. If there is one, we use Execute method to retrieve all the matches. There should be only one match.

After we define that, we can use this function in our MFC application. Note that you must initialize the COM library first in your application. The following will validate an input box to see if it matches the phone number format:

DDX_RegExp(pDX, IDC_INPUT, _T("\\d{3}-\\d{3}-\\d{4}"), m_strInput);

In this way, you can write validation code for more complicated pattern. The COM makes things a lot easier.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here

About the Author

Sherwood Hu



Location: United States United States

Other popular String handling articles:

Article Top
Sign Up to vote for this article
You must Sign In to use this message board.
FAQ FAQ Noise ToleranceSearch Search Messages 
 Layout  Per page   
 Msgs 1 to 8 of 8 (Total in Forum: 8) (Refresh)FirstPrevNext
GeneralGood work...memberbenny_thomas034:04 8 Jun '08  
GeneralCompiling the IDL?memberbiopsy17:27 21 Aug '07  
GeneralVC++ Project?membervito3334:41 2 Jun '05  
GeneralDistribution questonsussAnonymous12:44 14 May '05  
GeneralUser BrakpointsussAnonymous10:45 9 Nov '04  
GeneralGetting the headers for RegExp 1.0memberVince Gatto10:04 16 Sep '04  
GeneralRe: Getting the headers for RegExp 1.0sussbmaguire10:53 23 Sep '04  
GeneralRe: Getting the headers for RegExp 1.0sussVince Gatto10:27 26 Sep '04  

General General    News News    Question Question    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

PermaLink | Privacy | Terms of Use
Last Updated: 25 Jan 2001
Editor: Chris Maunder
Copyright 2000 by Sherwood Hu
Everything else Copyright © CodeProject, 1999-2008
Web18 | Advertise on the Code Project