1、C++正则表达式
// 头文件
#include <regex>
2、主要接口介绍
2.1、std::regex_match
此接口与其它语言的使用有区别,这里适用于字符串与正则表达式的全匹配,也就是验证字符串是否符合正则的规范。
2.2、std::regex_serch
此接口按照正则表达式在字符串中查找符合规范的字符串,不过每次调用只返回一个结果,若要返回多个匹配结果需反复调用。
3、范例
// 字符串转坐标对
// Point (123.00001 31.0384325)返回“123.00001 31.0384325”
void string2PointList(const std::wstring &pStr, std::vector<FileGDBAPI::Point*> &pPointList)
{
std::regex_constants::syntax_option_type fl = std::regex_constants::icase;
std::wregex pattern(L"(\\b(\\d*.\\d+\\s*))+", fl);
std::wsmatch result;
std::wstring tmpStr = pStr;
// 截取坐标对
while (std::regex_search(tmpStr, result, pattern)){
if (! result.empty()){
double x = 0.0, y = 0.0;
swscanf_s(result.str().c_str(), L"%16lf %16lf", &x, &y);
FileGDBAPI::Point *point = new FileGDBAPI::Point();
point->x = x;
point->y = y;
pPointList.push_back(point);
}
tmpStr = result.suffix().str();
}
}