boost::regex
已知字符串"google.com baidu.com",约定使用空格分割各域名,但域名的个数未知。
使用表达式1"(\\w+)",只能获得google
使用表达式2"(\\w+\\.\\w+)",只能获得google.com
我想依次获得google.com和baidu.com,在不知道baidu.com后面还有没有other.com的前提下,利用一个表达式,有可能办到吗?
对于已知字符串,我还要对各个域名进行验证,验证内容包括不允许两个连续的dot以及不允许没有dot。
===========
#include <iostream>
#include <string>
#include <boost/regex.hpp>
int main( ) {
boost::regex reg("??表达式怎么写,谢谢??", regbase::extended | regbase::icase);
std::string str = "google.com baidu.com";
boost::smatch matches;
string::const_iterator it = str.begin();
string::const_iterator end = str.end();
if (boost::regex_search(it, end, matches, reg)) { <== 你的表达式能否保证:如果str中一个域名不能通过验证,返回false。全部通过验证返回true。
for (int i = 1; i < matches.size(); ++i) { <== 你的表达式能否保证:如果str中所有域名都通过验证,这里可以依次获得每一个域名
cout << matches[i] << endl;
}
}
return 0;
}
如果你有更好的方法,请赐教。谢谢!!