在字符串内查找符合条件的字符串并打印
平台: xp
开发环境: vs2010
开发语言:c++
#include <string>
#include <regex>
//! 检查字符串是否是有效的IP地址及端口
//! @param fml_IP:包含IP地址信息的字符串
//! @param fml_port:包含端口号的字符串
//! @param iret:存储INT形式的IP地址
//! @return: 0正常/其他,核对对应的宏定义错误代码
extern int mg_CheckIP(const string& fml_IP, const string& fml_port, const int (*iret)[4][3]=NULL);
extern int mg_CheckIP( const string& fml_IP, const string& fml_port, const int (*iret)[4][3] )
{
int ret = 0;
/* 检查IP
* 1.定义匹配字符串
* 2.建立匹配对象
* 3.定义迭代器
* 4.获取结果数量
* 5.迭代输出结果 */
string pattern = "(\\d+)"; //要提取的元素
std::regex regPattern(pattern);
sregex_iterator iter(fml_IP.cbegin(), fml_IP.cend(), regPattern);
sregex_iterator iterEnd;
int n = std::distance(iter, iterEnd);
TRACE("check IP find %d words \n", n);
if(n != 4) goto err1; //IPv4地址由4段组成
for (sregex_iterator