Question
Does a given file name match a single - star pattern ?
index.html matches *.html
foo.txt does not match *.html
matches(“index.html”, “*html”) returns true
matches(“foo.txt”, “*html”) returns false
matches(“cat”, “c*t”) returns true
Solution
/*
============================================================================
Author : James Chen
Email : a.james.chen@gmail.com
Description : Match words.
Created Date : 20-01-2013
Last Modified :
============================================================================
*/
#include <iostream>
#include <iomanip>
#include <string>
#include <regex>
using namespace std;
bool Matches(string str1, string str2)
{
std::regex re(str2);
return regex_match(str1, re);
}
int main()
{
cout << std::boolalpha << Matches("index.html", ".*html") << endl;
cout << std::boolalpha << Matches("foo.txt", ".*html") << endl;
cout << std::boolalpha << Matches("cat", "c.*t") << endl;
return 0;
}
Output
true
false
true
Press any key to continue . . .