C++11正規表現例セット
6380 ワード
#include
#include
#include
using namespace std;
int testRegexSearch()
{
// ,
regex testRegex("[0-9]{2}:[0-9]{2}:[0-9]{2}\\.[0-9]{3}");
//
string strText("OTA LOG SFTCH/MPA Stream 2/Reservation Accept 07:23:50.580 Channel: 147, Pilot PN: 232");
// :cmatch
cmatch result;
//search , match
if (regex_search(strText.c_str(), result, testRegex, regex_constants::format_default))
{
cout << result.str() << endl;
}
else
{
cout << "fail." << endl;
}
return 0;
}
bool test_email_valid(const std::string& email)
{
const std::regex pattern("(\\w+)(\\.|_)?(\\w*)@(\\w+)(\\.(\\w+))+");
bool isValid = std::regex_match(email, pattern);
std::cout << email << " : " << (isValid ? "valid" : "invalid") << std::endl;
return isValid;
}
int testRegexMatch()
{
std::string email1 = "[email protected]";
std::string email2 = "[email protected]";
std::string email3 = "[email protected]";
std::string email4 = "marius@domain";
test_email_valid(email1);
test_email_valid(email2);
test_email_valid(email3);
test_email_valid(email4);
return 0;
}
void show_ip_parts(const std::string& ip)
{
// IP
const std::regex pattern("(\\d{1,3}):(\\d{1,3}):(\\d{1,3}):(\\d{1,3})");
// object that will contain the sequence of sub-matches
std:: match_results<:string::const_iterator> result;
// match the IP address with the regular expression
bool valid = std:: regex_match(ip, result, pattern);
std::cout << ip << " \t: " << (valid ? "valid" : "invalid") << std::endl;
// if the IP address matched the regex, then print the parts
if(valid)
{
std::cout << "b1: " << result[1] << std::endl;
std::cout << "b2: " << result[2] << std::endl;
std::cout << "b3: " << result[3] << std::endl;
std::cout << "b4: " << result[4] << std::endl;
}
}
int testMatchResult()
{
show_ip_parts("1:22:33:444");
show_ip_parts("1:22:33:4444");
show_ip_parts("100:200");
return 0;
}
int testMultiSearch()
{
//
const std::regex pattern("\\w+day");
//
std::string weekend = "Saturday and Sunday, but some Fridays also.";
//
const std::sregex_token_iterator end;
for (std::sregex_token_iterator it(weekend.begin(),weekend.end(), pattern); it != end ; ++it)
{
std::cout << *it << std::endl;
}
std::cout<<:endl return="" int="" regexreplace="" std::string="" text="This is a element and this a unique ID." regular="" expression="" with="" two="" capture="" groups="" const="" std::regex="" pattern="" the="" for="" transformation="" using="" second="" group="" replace="an $2" newtext="std::regex_replace(text," std::cout="" std::endl="" format_date="" date="" std::="" regex="" reverses="" position="" of="" all="" replacer="$5$4$3$2$1" apply="" tranformation="" regex_replace="" regexreplacedate="" date1="1/2/2008" date2="12.08.2008"> " << format_date(date1) << std::endl;
std::cout << date2 << " -> " << format_date(date2) << std::endl;
std::cout << std::endl;
return 0;
}
int searchCount() {
// "new" and "delete" ?
std::regex reg("(new)|(delete)");
std::smatch m;
std::string s = "Calls to new must be followed by delete. Calling simply new results in a leak!";
int new_counter=0;
int delete_counter=0;
std::string::const_iterator it=s.begin();
std::string::const_iterator end=s.end();
while (std::regex_search(it, end, m, reg))
{
// new delete?
m[1].matched ? ++new_counter : ++delete_counter;
it=m[0].second;
}
if (new_counter!=delete_counter) {
std::cout << "Leak detected!
";
}
else {
std::cout << "Seems ok...
";
}
std::cout << std::endl;
return 0;
}
int testRegexMain()
{
// 1) 07:23:50.580
cout << ">>>>>>>>>>>>>>testRegexSearch" << endl;
testRegexSearch();
// 2) email [email protected]
cout << ">>>>>>>>>>>>>>testRegexMatch" << endl;
testRegexMatch();
// 3) IP : 1:22:33:4444, IP 4
cout << ">>>>>>>>>>>>>>testMatchResult" << endl;
testMatchResult();
// 4) :Saturday
cout << ">>>>>>>>>>>>>>testMultiSearch" << endl;
testMultiSearch();
// 5) : a an
cout << ">>>>>>>>>>>>>>regexReplace" << endl;
regexReplace();
// 6) , DD-MM-YYYY –> YYYY-MM-DD
cout << ">>>>>>>>>>>>>>regexReplaceDate" << endl;
regexReplaceDate();
// 7) "new" and "delete" ?
cout << ">>>>>>>>>>>>>>searchCount" << endl;
searchCount();
return 0;
}