Thread: regex and c++

  1. #1
    Registered User Joelito's Avatar
    Join Date
    Mar 2005
    Location
    Tijuana, BC, México
    Posts
    310

    regex and c++

    Hi, I'm trying to output only a few files, using regex, from the current directory, this is my main code:
    Code:
    // main.cpp
    {
    		char * _dir = new char[MAX_PATH];
    		getcwd(_dir, MAX_PATH);
    		unique_ptr<CExtractor> _ptr(new CExtractor(_dir));
    		delete[] _dir;
    	}
    Code:
    // CExtractor.cpp
    DIR *pdir = NULL;
    	struct dirent *pent = NULL;
    	pdir = opendir(_cwd.c_str());
    	while ((pent = readdir(pdir)) && (pent->d_type != DT_DIR)) {
    		if (regex_match(pent->d_name, regex("\\.(in|txt)$"), std::regex_constants::icase)) {
    			cout << pent->d_name << endl;
    		}
    	}
    	closedir(pdir);
    My code is compiled correctly, but when I run my binary, nothings is outputed..here are my files in the current directpory:
    aclocal.m4 config.guess config.status depcomp m4 NEWS
    AUTHORS config.h config.sub INSTALL Makefile README
    autom4te.cache config.h.in configure install-sh Makefile.am src
    ChangeLog config.h.in~ configure.ac libtool Makefile.in stamp-h1
    compile config.log COPYING ltmain.sh missing
    As you can see, I have a few "in" files
    * PC: Intel Core 2 DUO E6550 @ 2.33 GHz with 2 GB RAM: Archlinux-i686 with xfce4.
    * Laptop: Intel Core 2 DUO T6600 @ 2.20 GHz with 4 GB RAM: Archlinux-x86-64 with xfce4.

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    37,682
    Have you done a simple test like
    Code:
            if (regex_match("foo.txt", regex("\\.(in|txt)$"), std::regex_constants::icase)) {
                cout << "foo.txt matches" << endl;
            }
    If that works, then examine pent->d_name in detail in the debugger.

    If it doesn't work, well then fix the literal test until it does work.
    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.

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. using regex
    By _izua_ in forum C++ Programming
    Replies: 4
    Last Post: 08-10-2007, 04:25 PM
  3. regex help
    By Jaqui in forum Tech Board
    Replies: 8
    Last Post: 10-14-2006, 03:53 PM
  4. regex
    By l2u in forum C++ Programming
    Replies: 8
    Last Post: 10-09-2006, 10:45 AM
  5. <regex.h> regex syntax in C
    By battersausage in forum C Programming
    Replies: 7
    Last Post: 03-24-2004, 01:35 PM