Thread: Matching regular expressions in c++

  1. #1
    Registered User
    Join Date
    Dec 2010
    Posts
    8

    Matching regular expressions in c++

    Hi all

    I have the following problem. I am using PERL language to read text file, and in this file I use pattern matching to search for specific line formatting. I need this because I am analyzing big code output.
    For instance if I look for line such as
    44.333e-02 44.221e-03 22.332e-04

    I can define in PERL


    Code:
    if($line=~/(\d{2}\.\d{3}e-\d{2}) +(\d{2}\.\d{3}e-\d{2}) +(\d{2}\.\d{3}e\d{2})/)
    {
    $a[$i++]=$1 ;
    
    $B[$j++]=$2 ;
    
    $C[$k++]=$3 ;
    
    }
    Where $1,$2,$3 are perl notation to say what is matched in the first second third bracket . This shall store the desired objects matched in the above specified pattern in @A,@B,@C arrays.
    Is it possible to do this in C++. In ordinary C there are functions regcomp() regexec() which are an option but are rather complicated compared with the simple perl procedure.

    Thank you in advance

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Like this perhaps?
    Boost.Regex
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Dec 2010
    Posts
    8
    This is ok, but can't I manage my goal with standard package libraries. Because after all I would like to do it as simple as possible.

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Sure you can, but rewriting regex from scratch won't be that easy, unless you're willing to compromise on a lot of features.

    Or you could take a less literal translation of Perl to C++, and instead write a custom validation routine for a string with two characters, followed by dot, followed by...

    What exactly are you hoping to get by doing this in C++, which you can't do in Perl?
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  5. #5
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by wronski11
    This is ok, but can't I manage my goal with standard package libraries.
    There is no regex support in the C++ standard library at the moment, hence you would have to implement it yourself. That said, you may be able to use std::tr1::regex (i.e., regex support from the TR1 extensions to the standard library), or even std::regex (as an "early adopter": it is pretty much the same as std::tr1::regex anyway, which was adapted from Boost.Regex).
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  6. #6
    Registered User
    Join Date
    Dec 2010
    Posts
    8
    Well first of all thank you for the very professional opinion. I installed the boost.regex library but got problems compiling c++ code which uses it. I used the following command to compile my code

    Code:
    g++ -o ZZ test.cpp –I/home/boost/boost_1_45_0/libs/regex/build/gcc/boost_regex-gcc-1_45/ -L/home/boost/boost_1_45_0/libs/regex/build/gcc/libboost_regex-gcc-1_45.a
    In short I am giving the paths to de library and the includes . However it does not run. The actual code i compile is : (code downloaded form the internet)

    Code:
    #include <iostream>
    #include <string>
    #include <boost/regex.hpp>
    
    using namespace std;
    
    int main( ) {
    
       std::string s, sre;
       boost::regex re;
    
       while(true)
       {
          cout << "Expression: ";
          cin >> sre;
          if (sre == "quit")
          {
             break;
          }
          cout << "String:     ";
          cin >> s;
    
          try
          {
             // Set up the regular expression for case-insensitivity
             re.assign(sre, boost::regex_constants::icase);
          }
          catch (boost::regex_error& e)
          {
             cout << sre << " is not a valid regular expression: \""
                  << e.what() << "\"" << endl;
             continue;
          }
          if (boost::regex_match(s, re))
          {
             cout << re << " matches " << s << endl;
          }
       }
    }
    Does anyone knows why this does not work?


    The actual error reads as follows


    Code:
    ccYuUtqf.o: In function `boost::basic_regex<char, boost::regex_traits<char, boost::cpp_regex_traits<char> > >::assign(char const*, char const*, unsigned int)':
    test.cpp:(.text._ZN5boost11basic_regexIcNS_12regex_traitsIcNS_16cpp_regex_traitsIcEEEEE6assignEPKcS7_j[boost::basic_regex<char, boost::regex_traits<char, boost::cpp_regex_traits'
    /home/tmp/ccYuUtqf.o: In function `boost::re_detail::perl_matcher<__gnu_cxx::__normal_iterator<char const*, std::basic_string<char, std::char_traits<char>, std::allocator:
    test.cpp:(.text._ZN5boost9re_detail12perl_matcherIN9__gnu_cxx17__normal_iteratorIPKcSsEESaINS_9sub_matchIS6_EEENS_12regex_traitsIcNS_16cpp_regex_traitsIcEEEEEC1ES6_S6_RNS_13matc'
    /home/tmp/ccYuUtqf.o:(.gcc_except_table+0xa4): undefined reference to `typeinfo for boost::regex_error'
    /home/tmp/ccYuUtqf.o: In function `bool boost::regex_match<__gnu_cxx::__normal_iterator<char const*, std::basic_string<char, std::char_traits<char>, std::allocator<char> :
    test.cpp:(.text._ZN5boost11regex_matchIN9__gnu_cxx17__normal_iteratorIPKcSsEESaINS_9sub_matchIS5_EEEcNS_12regex_traitsIcNS_16cpp_regex_traitsIcEEEEEEbT_SD_RNS_13match_resultsISD'
    collect2: ld returned 1 exit status

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Regular expressions [Boost]
    By Desolation in forum C++ Programming
    Replies: 8
    Last Post: 12-30-2006, 10:10 PM
  2. Regular expressions
    By JimpsEd in forum C Programming
    Replies: 5
    Last Post: 05-13-2006, 06:01 PM
  3. Help please: regular expressions in C++
    By reivaj7999 in forum C++ Programming
    Replies: 3
    Last Post: 08-24-2005, 01:11 AM
  4. Regular expressions
    By jverkoey in forum A Brief History of Cprogramming.com
    Replies: 9
    Last Post: 01-23-2005, 09:36 PM
  5. Going out of scope
    By nickname_changed in forum C++ Programming
    Replies: 9
    Last Post: 10-12-2003, 06:27 PM