CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 2 of 2
  1. #1
    Join Date
    Apr 2008
    Posts
    3

    [RESOLVED] Boost Regex Help

    Hi everyone,
    I'm working on a project in C++ and I needed regex support so I setup the Boost libraries. The part of the code I am having trouble with is when it is supposed to read in a text file, then output the lines formatted like this: " 12345..." So the lines I am looking for start with 2 spaces followed by 5 digits. For those of you who know Perl, I ran a small script with the following regex code and it worked fine:
    Code:
    if ($_ =~ /^  \d{5}/) ...
    However when I translate it to C++ using the Boost libraries, it doesn't work. My code is posted below. Can someone please tell me what is wrong here? Thanks in advance for your help. Note I am running the file as such:
    Code:
    ./example < test.txt
    Code:
    #include <boost/regex.hpp>
    #include <iostream>
    #include <string>
    #include <fstream>
    using namespace std;
    
    int main() {
       string line;
       boost::regex pat("^  \\d{5}");
    
       while (cin) {
          getline(cin, line);
          if (boost::regex_match(line,pat)) {
             cout<<line<<endl;
          }
       }
       return 0;
    }

  2. #2
    Join Date
    Apr 2008
    Posts
    3

    Re: Boost Regex Help

    Well to answer my own question... I was supposed to be using boost:regex_search instead of boost::regex_match. Apparently match tries to match the whole input while search actually searches for the expression, who knew? Haha.

    Thanks anyways.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured