$include_dir="/home/hyper-archives/boost-users/include"; include("$include_dir/msg-header.inc") ?>
From: Alexis H. Rivera-Rios (ahrivera_at_[hidden])
Date: 2005-03-28 16:34:37
Hi,
I'm playing around with the boost::regex library and I
want to match two tokens from a filename.  For example
files with the following format:
myname_123.dat
I want to be able to determine the string and number
separated by the underscore.  So the result of my
match should be "myname", and 123.
In python, I can do it like this:
import re
>>> x = 'myclass_123.dat'
>>> result = re.match(r'(\w+)_(\d+)\.dat$', x)
>>> result.group(1)
'myclass'
>>> result.group(2)
'123'
I'm having problems getting the same result in C++. 
This is the sample program:
int main(int argc, const char** argv)
{
   boost::regex expression("(\\w+)_(\\d+)\\.dat$");   
    
   std::string filename(argv[1]);
   boost::cmatch what; 
                  
    if(boost::regex_search(filename.c_str(), what,
expression)) 
    {
        for (int i=0;i<what.size();++i)
        {
           std::cout << what[i].first << " " 
                     << what[i].second << std::endl;	
        }
   }
   return 0;
}
The output of this program is:
> openmp3 alexis_1023.dat
alexis_1023.dat
alexis_1023.dat _1023.dat
1023.dat .dat
How do I get my groups "alexis", 123 as in Python?
Thanks,
Alexis
Programming Tutorial:
In Python: To do this, do this
In Perl: To do this, do this or this or this or this...
In C: To do this, do this, but be careful
In C++: To do this, do this, but don't do this, be careful of this, watch out for this, and whatever you do, don't do this
                
__________________________________ 
Do you Yahoo!? 
Yahoo! Small Business - Try our new resources site!
http://smallbusiness.yahoo.com/resources/