Click here to Skip to main content
15,444,626 members
Articles / Desktop Programming / MFC
Posted 3 Jan 2006


32 bookmarked

GRETA - How to use Regular Expressions in Visual C++ correctly

Rate me:
Please Sign up or sign in to vote.
4.18/5 (12 votes)
3 Jan 2006CPOL3 min read
Introduction to building GRETA in Visual C++.


GRETA gives you all the power of PERL 5 regular expressions in your C++ applications. This is the homepage of GRETA. Today, we will discuss neither regular expressions nor template classes in the GRETA library, because you can get familiar with them at the GRETA homepage. Here, I will explain how to add and build the GRETA library in Visual C++, step by step.


Download GRETA's source code from GRETA homepage. For example: GRETA 2.6.4 for VC6, you will get ''. Or, you can download it from this site. Please do not download the *.lib file for GRETA (such as greta.lib etc.). You may find GRETA in the *.lib file from Microsoft. But greta.lib may not satisfy your needs. It may generate link errors.


GRETA library has six program files:

  • regexpr2.h
  • regexpr2.cpp
  • syntax2.h
  • syntax2.cpp
  • restack.h
  • reimpl2.h

Create a new 'Win32 Static Library' project for GRETA in your workspace:

  • Step 1: Create a blank workspace. For example, 'Test':

    Image 1

  • Step 2: Create the main project that will use GRETA. For example, 'Console' type 'Main':

    Image 2

  • Step 3: Create the GRETA project. Select 'Win32 Static Library' and make it a dependent of the Main project. After this, press the OK button, let the 'Pre-Compiled header' and 'MFC Support' remain unchecked:

    Image 3

  • Step 4: Copy those six program files from the GRETA zip package into the GRETA project's directory and add them to the project:

    Image 4

  • Step 5: Copy one of the example codes into the main() function. Change #include "regexpr2.h" to #include "..\greta\regexpr2.h". These examples are in the HTML file of the GRETA zip package. Select 'Main' project as the active project, then compile and run the project:

    Image 5

This is the final result that you get:

Image 6

Now, we have successfully finished the project with GRETA. [Download example project].

If your project uses Unicode or your main project is DLL type, please go on with the following.

Unicode version

If your project (which uses GRETA) is of Unicode version, you must make the GRETA project to be of Unicode version. For example:

  • Step 1: Add a 'Unicode Debug' configuration for your GRETA project:

    Image 7

  • Step 2: In the project settings, add '_UNICODE' and 'UNICODE' macros into the C++ general preprocessor definitions of the corresponding 'Unicode Debug' configuration:

    Image 8

  • Step 3: Repeat step 1 and step 2 for the 'Main' project as well as for 'Release' configuration.

You will get 'Unicode Debug' and 'Unicode Release' configurations for both GRETA and your main project.

Runtime library

In many cases, when your main project is DLL type, you must make sure that GRETA uses the same runtime library as your main project, or you will get a link error.

  • Step 1: To find out which runtime library your main project uses:

    Image 9

  • Step 2: Select the same runtime library for GRETA project. Now, you can link successfully.

Q: Why do we need to download greta.lib and why the downloaded greta.lib generates link error?

A: This is because of "Unicode version" and "Runtime library". Because it is not known whether the downloaded greta.lib is of Unicode version, and it is also not known which runtime library the downloaded greta.lib uses, so it may generate link errors.

Regular expression tools


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Written By
Software Developer (Senior)
China China
Begin coding from basic, since 1994. Interested in coding and database and website constructing.
My website: - Regular Expression Laboratory
The easiest regex engine:

Comments and Discussions

GeneralRe: CAtlRegExp Pin
Igor_Solovyov_10-Jan-06 10:13
MemberIgor_Solovyov_10-Jan-06 10:13 
AnswerRe: CAtlRegExp Pin
sswater shi10-Jan-06 17:19
Membersswater shi10-Jan-06 17:19 
QuestionCan't download greta project Pin
Stlan4-Jan-06 3:44
MemberStlan4-Jan-06 3:44 
AnswerRe: Can't download greta project Pin
sswater shi4-Jan-06 13:54
Membersswater shi4-Jan-06 13:54 
GeneralRe: Can't download greta project Pin
Stlan4-Jan-06 19:21
MemberStlan4-Jan-06 19:21 
Generalboost Pin
Goran Mitrovic3-Jan-06 21:36
MemberGoran Mitrovic3-Jan-06 21:36 
If I'm not mistaken, Eric Niebler made a new regexp engine, based only on header files and it has just been accepted into the Boost.

- Goran.

GeneralThank you very much! Pin
sswater shi3-Jan-06 21:46
Membersswater shi3-Jan-06 21:46 
GeneralRe: boost Pin
Shawn Poulson4-Jan-06 2:03
MemberShawn Poulson4-Jan-06 2:03 
GeneralRe: boost Pin
Goran Mitrovic4-Jan-06 2:14
MemberGoran Mitrovic4-Jan-06 2:14 
GeneralVery Good Pin
lxdff3-Jan-06 21:08
Memberlxdff3-Jan-06 21:08 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.