Ok I need some fast help with this, I'm trying to get a regular expression to work with my file system. this is what i currently have, but it seem to crash on, boost::regex expr(regex);

void show_files (const path & directory, bool recurse_into_subdirs, bool lSwitch, bool rSwitch, bool aSwitch, string regex)
{
	cout << "in function" << endl;
	boost::regex expr(regex);
	cout << "set regex" << endl;
	if(exists(directory))
    {
      directory_iterator end ;
      for(directory_iterator iter(directory) ; iter != end ; ++iter)
        if (!is_directory(*iter))
        {
			cout << "in file if" << endl;
			string s(iter->leaf());
			cout << "set string" << endl;
			bool isMatch = boost::regex_match(s, expr);
			cout << "matched regex" << endl;
			if(isMatch == true){
				cout << "in isMatch if" << endl;
				cout << (lSwitch == true?"\n-: ":"\n ") << setw(15) << right << file_size(iter->path()) << "  " << iter->leaf() ;
				cout << "finsihed" << endl;
			}
        }
        else
		{
			cout << (lSwitch == true?"\nd: ":"\n ") << setw(15) << right << "" << "  " << iter->leaf() << "/" ;
			if(recurse_into_subdirs) show_files(*iter, recurse_into_subdirs, lSwitch, rSwitch, aSwitch, regex) ;
		}
    }
}

Does your project require boost? I'd just avoid it altogether.

Yes, i must use boost.

Ok well i'm having a little problem, i'm trying to check if the file and the expression are a match, but it keeps returning false.

void show_files (const path & directory, bool recurse_into_subdirs, bool lSwitch, bool rSwitch, bool aSwitch, string regex)
{
	bool allFiles = false;
	boost::regex expr("");
	if(regex != "*.*")
	{
		boost::regex expr(regex);
	}
	else{
		allFiles = true;
	}
	if(exists(directory))
    {
      directory_iterator end ;
      for(directory_iterator iter(directory) ; iter != end ; ++iter)
        if (!is_directory(*iter))
        {
			if(!allFiles)
			{
				string s(iter->leaf());
				bool isMatch = boost::regex_match(s, expr);
				if(isMatch == true)
				{
					cout << (lSwitch == true?"\n-: ":"\n ") << setw(15) << right << file_size(iter->path()) << "  " << iter->leaf() ;
				}
				else
					cout << "file not found" << endl;
			}
			else
				cout << (lSwitch == true?"\n-: ":"\n ") << setw(15) << right << file_size(iter->path()) << "  " << iter->leaf() ;
        }
        else
		{
			cout << (lSwitch == true?"\nd: ":"\n ") << setw(15) << right << "" << "  " << iter->leaf() << "/" ;
			if(recurse_into_subdirs) show_files(*iter, recurse_into_subdirs, lSwitch, rSwitch, aSwitch, regex) ;
		}
    }
}

Ok well i'm having a little problem, i'm trying to check if the file and the expression are a match, but it keeps returning false.

void show_files (const path & directory, bool recurse_into_subdirs, bool lSwitch, bool rSwitch, bool aSwitch, string regex)
{
	bool allFiles = false;
	boost::regex expr("");
	if(regex != "*.*")
	{
		boost::regex expr(regex);
	}
	else{
		allFiles = true;
	}
	if(exists(directory))
    {
      directory_iterator end ;
      for(directory_iterator iter(directory) ; iter != end ; ++iter)
        if (!is_directory(*iter))
        {
			if(!allFiles)
			{
				string s(iter->leaf());
				bool isMatch = boost::regex_match(s, expr);
				if(isMatch == true)
				{
					cout << (lSwitch == true?"\n-: ":"\n ") << setw(15) << right << file_size(iter->path()) << "  " << iter->leaf() ;
				}
				else
					cout << "file not found" << endl;
			}
			else
				cout << (lSwitch == true?"\n-: ":"\n ") << setw(15) << right << file_size(iter->path()) << "  " << iter->leaf() ;
        }
        else
		{
			cout << (lSwitch == true?"\nd: ":"\n ") << setw(15) << right << "" << "  " << iter->leaf() << "/" ;
			if(recurse_into_subdirs) show_files(*iter, recurse_into_subdirs, lSwitch, rSwitch, aSwitch, regex) ;
		}
    }
}
boost::regex expr("");
	if(regex != "*.*")
	{
		boost::regex expr(regex);
	}

You're redeclaring a variable named expr, thus at the end of the if statement, it goes out of scope and you are left with boost::regex expr("");, not the one you wanted.

commented: THANK YOU!!! saved me in the last min! +1

OMG! Thank you so much! 30 min till have have to hand it in and you just saved me like 30% of my grade right there! I LOVE YOU! lol

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.