C++ regex

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();
	}
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值