Click to See Complete Forum and Search --> : [RESOLVED] Boost Regex Help


rscott5
April 16th, 2008, 06:23 PM
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:
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: ./example < test.txt


#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;
}

rscott5
April 16th, 2008, 10:52 PM
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.