// std_tr1__regex__regex_replace.cpp
// compile with: /EHsc
#include <regex>
#include <iostream>
int main()
{
char buf[20];
const char *first = "axayaz";
const char *last = first + strlen(first);
std::regex rx("a");
std::string fmt("A");
std::regex_constants::match_flag_type fonly =
std::regex_constants::format_first_only;
*std::regex_replace(&buf[0], first, last, rx, fmt) = '\0';
std::cout << "replacement == " << &buf[0] << std::endl;
*std::regex_replace(&buf[0], first, last, rx, fmt, fonly) = '\0';
std::cout << "replacement == " << &buf[0] << std::endl;
std::string str("adaeaf");
std::cout << "replacement == "
<< std::regex_replace(str, rx, fmt) << std::endl;
std::cout << "replacement == "
<< std::regex_replace(str, rx, fmt, fonly) << std::endl;
return (0);
}
<span id="mt23" class="sentence" data-guid="0ec2a73fd7a67cad7903a59a5c4d96ee" data-source="" replacement="=" axayaz"="" xml:space="preserve">替换 AxAyAz ==
替换 Axayaz ==
替换 AdAeAf ==
替换 Adaeaf ==
// std_tr1__regex__regex_replace.cpp
// compile with: /EHsc
#include <regex>
#include <iostream>
int main()
{
char buf[20];
const char *first = "axayaz";
const char *last = first + strlen(first);
std::regex rx("a");
std::string fmt("A");
std::regex_constants::match_flag_type fonly =
std::regex_constants::format_first_only;
*std::regex_replace(&buf[0], first, last, rx, fmt) = '\0';
std::cout << "replacement == " << &buf[0] << std::endl;
*std::regex_replace(&buf[0], first, last, rx, fmt, fonly) = '\0';
std::cout << "replacement == " << &buf[0] << std::endl;
std::string str("adaeaf");
std::cout << "replacement == "
<< std::regex_replace(str, rx, fmt) << std::endl;
std::cout << "replacement == "
<< std::regex_replace(str, rx, fmt, fonly) << std::endl;
return (0);
}
替换 AxAyAz ==
替换 Axayaz ==
替换 AdAeAf ==
替换 Adaeaf ==