C++正则匹配字符串
正则示例
以下实例使用C++正则从一串混乱的字符串中匹配带小数点的数字
点击查看代码
#include <iostream>
#include <regex>
using namespace std;
int main()
{
smatch results;
string str = "adbhjasdhaj1231.123QWEE QWEQWWQEDXQ 12346.4156";
string pat("\\d+\\.\\d+");
regex r(pat);
// 方法1:
for (sregex_iterator it(str.begin(), str.end(), r), end_it; it != end_it; ++it)
{
cout << it->str() << endl;
}
return 0;
}
运行后可得到以下内容:
我的C++环境参考配置教程
https://www.cnblogs.com/dapenson/p/15992769.html
封装调用示例
点击查看代码
#include <iostream>
#include <regex>
using namespace std;
vector<string> get_match(string str)
{
vector<string> str_out;
smatch results;
string pat("\\d+\\.\\d+");
regex r(pat);
for (sregex_iterator it(str.begin(), str.end(), r), end_it; it != end_it; ++it)
{
cout << it->str() << endl;
str_out.push_back(it->str());
}
return str_out;
}
int main()
{
string str_test = "asdajsdk123123.123123dhja";
vector<string> str_results = get_match(str_test);
for (vector<string>::iterator iter = str_results.begin(); iter > str_results.end(); iter++)
{
cout << *iter << endl;
}
cout << str_results.size() << endl;
return 0;
}
检查 C++ 标准编译器的版本
点击查看代码
#include<iostream>
using namespace std;
int main() {
if (__cplusplus == 201703L)
std::cout << "C++17" << endl;
else if (__cplusplus == 201402L)
std::cout << "C++14" << endl;
else if (__cplusplus == 201103L)
std::cout << "C++11" << endl;
else if (__cplusplus == 199711L)
std::cout << "C++98" << endl;
else
std::cout << "pre-standard C++" << endl;
}