Thread: How to apply Regex in C++?

  1. #1
    Registered User
    Join Date
    Aug 2013
    Posts
    30

    How to apply Regex in C++?

    Hello to all,

    I'd like to extract some patterns from a string using regex. I found that regex in C++ are supported only in C++ 11 and
    searching about this, I found that it seems only almost Microsoft Visual Studio 2012 C++ compiler supports this.

    Can I use the MSVisual Studio C++ compiler in other IDE like netbeans, Dev C++, Code blocks etc or only in MS Visual Studio?

    Or do you know about another compiler that supports Regex in C++?

    Thanks in advance.

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Are you trying to use Regex in your actual program, or just when you are typing your code? Since you mention a bunch of IDEs I'm not sure.

    My Google-fu may be weak, but I am only finding the latter for MSVS. If you've got a link to where they describe regex in code I'd like to see it. (EDIT: Obvs it's part of the 11 features. My Google-fu must be very weak today.)

    There's certainly regex.h on a *nix machine, of course. (It's possible that might be available to Windows using mingw or msys, but I couldn't find it on a quick search on my machine.) I don't know how up-to-date mingw is with C++11 (I haven't bothered to update mine in a while).
    Last edited by tabstop; 10-14-2013 at 05:33 PM.

  3. #3
    [](){}(); manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    *nullptr
    Posts
    2,657
    Quote Originally Posted by tabstop View Post
    ) I don't know how up-to-date mingw is with C++11 (I haven't bothered to update mine in a while).
    Since mingw is based on GCC, the <regex> header will be there, but any attempt to use it will result in an exception being thrown.
    Clang has it implemented though.
    Another option is Boost.Regex.

  4. #4
    Registered User
    Join Date
    Mar 2013
    Posts
    63
    I use pcre3 for all my needs. There is a nice wrapper for C++ too.
    The nuwen distro MinGW Distro - nuwen.net has it as part of the distro.
    I link with the static library so no dll to lug around.


    James

  5. #5
    Registered User
    Join Date
    Mar 2013
    Posts
    63
    This example parses a Windows resource dialog file (rc) in the format created with:
    https://fbedit.svn.sourceforge.net/s...Ed22/ResEd.zip
    This editor allows the style and exestyle to be saved as hex.


    It was tested with the MINGW distro from nuwen as it has pcre3 included.
    MinGW Distro - nuwen.net


    I also tested with the TDM-GCC distro but I had to build the pcre3 library.
    I am not too versed with the pcre3 library, just enough to use it a bit.
    I tried to show here a bit of the C++ wrapper.


    James


    Code:
    //=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*
    // tested with NUWEN and TDM-GCC compilers
    // make  sure to use -lpcrecpp -lpcre -DPCRE_STATIC when compiling
    //=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*
    #include <iostream>
    #include <fstream>
    #include <algorithm>
    using namespace std;
    #include "pcrecpp.h"
    #define WAITKEY system("pause")
    void cls (void)
    {
      system("cls");
    }
    //==============================================================================
    int main ()
    {
      vector<string>  Linesvec;
      string  sFileIn="DLG2SRC.rc";
      string  sLine;
      string  sStyle;
      string  sExStyle;
      int     Start={0};
      int     rc={0};
      int     i={0};
      pcrecpp::RE*  re[3];
      string   S1[10];
      ifstream  f;
    
    
    //id name and value 
      re[0]= new  pcrecpp::RE("(?i)#define\\s*(\\w+)\\s*(\\d+)");
    //'Dialog info 
      re[1]= new  pcrecpp::RE("(?i)\\s*(\\w*)\\s*(DIALOGEX|DIALOG)\\s*(\\d+)\\s*,\\s*(\\d+)\\s*,\\s*(\\d+)\\s*,\\s*(\\d+)");
    // Control Information  
      re[2]= new  pcrecpp::RE("(?i)\\s*CONTROL\\s+\\x22\\s*([^\\x22\\r\\n]*)\\s*\\x22\\s*,\\s*(\\w*)\\s*,\\x22(\\s*\\w+)\\x22\\s*,([a-fx0-9]+)\\s*,(\\d+)\\s*,\\s*(\\d+)\\s*,\\s*(\\d+)\\s*,+\\s*(\\d+)\\s*(,+([a-fx0-9]*)\\s*)?");
    
    
    // Open File
      f.open(sFileIn);
    // Is it open? if not time to exit
      if(!f.is_open())
        {
          cout  <<  "Could not locate "  <<  sFileIn  <<  endl;
          WAITKEY;
          return 0;
        }
        
    // Get each line and stuff it in a vector
      while(getline(f,sLine))
        {
          Linesvec.push_back(sLine);
        }
    
    
    //close the rc file
      f.close();
    
    
    /*
    ------------------------------------------------------------------------------
    Get Id names and values from rc file
    ------------------------------------------------------------------------------
    */
      cout  <<  " Control Dialog Names and Values"  <<  endl;
      cout  <<  "----------------------------------"  <<  endl;
    
    
    // C++11 range based for with auto data type detection for iterator
    
    
     for(auto it : Linesvec)
        {
          rc= re[0]->FullMatch( it,  &S1[0],  &S1[1]);
          if(rc )
            {
              cout  <<  S1[0]  <<  " = "  <<  S1[1]  <<  endl;
            }
        }
    
    
      WAITKEY;
      cls();
     for( auto it : Linesvec)
        {
          rc= re[1]->FullMatch( it,  &S1[0],  &S1[1],  &S1[2],  &S1[3],  &S1[4],  &S1[5]);
          if(rc )
            {
              cout << "----------------------------------" << endl;
              cout << "Dialog Information" << endl;
              cout << "----------------------------------" << endl;
              cout << "Dialog Name   " << S1[0] << endl;
              cout << "Dialog Type   " << S1[1] << endl;
              cout << "Dialog Left   " << S1[2] << endl;
              cout << "Dialog Top    " << S1[3] << endl;
              cout << "Dialog Width  " << S1[4] << endl;
              cout << "Dialog Height " << S1[5] << endl;
            }
        }
    
    
      WAITKEY;
      cls();
    
    
    // Control Information
    
    
    
    
     for( auto it : Linesvec)
        {
          rc= re[2]->FullMatch( it,  &S1[0],  &S1[1],  &S1[2],  &S1[3],  &S1[4],  &S1[5],  &S1[6],  &S1[7],  &S1[8],  &S1[9]);
          if(rc )
            {
              cout << "----------------------------------" << endl;
              cout << "Control Information" << endl;
              cout << "----------------------------------" << endl;
              cout << "Id:        " << S1[1] << endl;
              cout << "Text:      " << S1[0] << endl;
              cout << "Class:     " << S1[2] << endl;
              cout << "Style:     " << S1[3] << endl;
              cout << "Left:      " << S1[4] << endl;
              cout << "Top:       " << S1[5] << endl;
              cout << "Width:     " << S1[6] << endl;
              cout << "Height:    " << S1[7] << endl;
              if(S1[9].length())
                {
                  cout << "Exe Style: " << S1[9] << endl;
                }
              WAITKEY;
              cls();
            }
        }
    
    
      for(i=0; i<=2; i+=1)
        {
          delete re[i];
        }
    
    
      return 0;
    }
    Attached Files Attached Files

  6. #6
    Registered User
    Join Date
    Aug 2013
    Posts
    30
    Hello jcfuller,

    Thanks for the help and example shared. I'll check your suggestions as soon as possible. I hope it works for me .

    Best regards

  7. #7
    Registered User
    Join Date
    Oct 2013
    Posts
    1
    Hello Mestertik,

    Regex was introduced with TR1.
    Microsoft Visual Studio 2008 support regular expressions.
    TR1 Regular Expressions

  8. #8
    Registered User
    Join Date
    Aug 2013
    Posts
    30
    Hello kasparov,

    Thank you for your help, I'll check the link you share. I hope the regex used by TR1 are standard like regex in Perl.

    Hello jcfuller,

    I've been trying to install the MinGW distro you suggest me, all goes fine until I try "g++ --version".

    I get the error:
    Code:
    C:\MinGW\bin\g++.exe is not a valid Win32 application.
    I'm testing on windows XP.

    Thanks again.

  9. #9
    Registered User
    Join Date
    Mar 2013
    Posts
    63
    For XP you will need to scroll down to the History item and download the 10.4 release. This was his last 32bit version.

    James

  10. #10
    Registered User Vespasian's Avatar
    Join Date
    Aug 2011
    Posts
    181
    The Boost Library has Regex functionality:

    Boost C++ Libraries

  11. #11
    Registered User
    Join Date
    Aug 2013
    Posts
    30
    Hello jcfuller,

    Thank you for your help. I've downloaded MinGW, but I don't know how to build pcre3 files. How did you do it? Since it is marked with error the line "#include prcecpp.h".

    Hello Vespasian,

    Thank you for the answer and share the link. I've unzipped but I still seeing how to make it work with netbeans and see how to use the regex library.

    One question, boost libraries are portable, I mean that once compiled a project I won't need to have boost in another machine to run a program?

    Thanks for the help.

  12. #12
    Registered User Vespasian's Avatar
    Join Date
    Aug 2011
    Posts
    181
    Quote Originally Posted by Mestertik View Post
    Hello Vespasian,

    Thank you for the answer and share the link. I've unzipped but I still seeing how to make it work with netbeans and see how to use the regex library.
    1) Download the official windows Boost.zip library (Version 1.54.0) and extract it into the following folder on computer: C:\boost_1_54_0

    2) Open CodeBlocks, create a new project and main cpp source file. (We'll be using CodeBlocks as the IDE, and once you get the concept, it should be the same for all IDE's)

    3) Tell the compiler where to find headers and library files: Set up global compiler path directories for the boost library by opening settings -> compiler and debugger -> tab search directories -> sub tab compiler -> add button and entering the search directory: C:\boost_1_54_0

    4) Tell the linker where to find headers and library files: Set up global linker path directories for the boost library by opening settings -> compiler and debugger -> tab search directories -> sub tab compiler -> add button and entering the search directory: C:\boost_1_54_0

    5) Tell the linker which libraries the programme is using: Right click my project on the left hand side pane and select build options. I click the linker tab. Under Linker Settings I press add and then I add the library I wish to use: C:\boost_1_54_0\ This step shouldnt be necassary as all boost files are header based and there are no libraries.

    6) In your source file code, use the correct header. For instamce if you wanted to use the find function, type in:
    Code:
    #include <boost\algorithm\string\find.hpp
    Quote Originally Posted by Mestertik View Post
    One question, boost libraries are portable, I mean that once compiled a project I won't need to have boost in another machine to run a program?

    Thanks for the help.
    The Boost library that you download is statically linked to at compile time, meaning, the library code is stored within the executable rather than in separate files like, say, Dynamic link libraries. So you wouldn't need Boost library on another machine.
    Last edited by Vespasian; 10-22-2013 at 01:57 AM.

  13. #13
    Registered User
    Join Date
    Mar 2013
    Posts
    63
    Personally I find the boost libraries beyond my novice c++ skills.
    I have used pcre3 for years in a number of languages and feel more comfortable with it.


    If you are using the nuwen distro the boost libraries are included so there is no downloading needed if you choose this route.
    For pcre3:
    If you extracted the nuwen distro to mingw change the name. For the 32bit (10.4) I use MinGw104 or Nuwen32.
    In code blocks: Menu -> Settings -> Toolchain set the executables Compiler's installation directory to the nuwen directory.
    In code blocks: Menu -> Settings linker settings add pcrecpp and pcre to the link libraries.

  14. #14
    Registered User
    Join Date
    Aug 2013
    Posts
    30
    Hello Vespasian,

    Thank you for the detailed explanation. I'll follow to try to set boost on codeblocks. I hope will work fine since with jcfuller Mingw distro pcre worked.

    Hello jcfuller,

    Thank you. I was able to run your code doing as you said in codeblocks. I was not able to do it in netbeans. Anyway I need to see why.

  15. #15
    Registered User
    Join Date
    Aug 2013
    Posts
    30
    Hello guys,

    I was able to apply the procedure to use those libraries in codeblocks, but I was tryin in Visual Studio and it seems the options are very different.

    Do you know how to add boost and pcre o use it in Visual Studio (I tried wit VS 2012, 2013)

    I just don't find the same options to add lib and include files in VS.

    Thanks in advance

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. regex in c (posix regex)
    By baxy in forum C Programming
    Replies: 1
    Last Post: 11-16-2012, 01:15 PM
  2. Program to apply windows registry settings
    By john3j in forum C Programming
    Replies: 5
    Last Post: 09-21-2012, 08:08 PM
  3. Apply style to multiple windows
    By gaurav_13191 in forum Windows Programming
    Replies: 5
    Last Post: 07-07-2011, 11:59 PM
  4. How to apply selection sort in a two-dimensional array
    By yigster in forum C++ Programming
    Replies: 16
    Last Post: 04-28-2010, 11:56 AM
  5. <regex.h> regex syntax in C
    By battersausage in forum C Programming
    Replies: 7
    Last Post: 03-24-2004, 01:35 PM