#include <regex>
#include <string>
// 使用类 std::regex_iterator 来进行多次搜索.
static std::string _strs = "... ..." ;
std::regex _regex("<img [^>]+>");
std::cout << "sregex_iterator ====" << std::endl;
auto words_begin =
std::sregex_iterator(_strs.begin(), _strs.end(), _regex);
auto words_end = std::sregex_iterator();
for (std::sregex_iterator i = words_begin; i != words_end; ++i)
{
std::smatch match = *i;
std::string match_str = match.str();
std::cout << match_str << '\n';
}
// 把所有 img src 的绝对路径替换为 images 开始的相对路径.使用分组即可.
std::regex img_regex("(<img [^>]*src=[\"']{1})([^\"']*)\\\\(images\\\\[^\"']*[\"']{1}[^>]*>)");
std::smatch color_match;
std::string rep = "$1$3";
std::string tmp = std::regex_replace(kHtmlSnippet,img_regex,rep);
std::cout << tmp << std::endl;
获取以-成对的字符串
\w+\s*-(\s*[^,;]+)
样本:
fsd-fsdfs fds-fsdfs,werfsd-fewrwrw;fewrwer,fdf -fdf fsdew- fd90
匹配结果:
fsd-fsdfs
fds-fsdfs
werfsd-fewrwrw
fdf -fdf
fsdew- fd90
另一小例
[^,"]+|,,|(?:"[^,"]*"[^"]*"[^"]*)"|"(?:[^"])*"
样本: your dollors,10000, 27 years old ,,"10,000","it is "10 Grand",baby",10k
匹配结果: your dollors 10000 27 years old ,, "10,000" "it is "10 Grand",baby" 10k
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。
扫码关注腾讯云开发者
领取腾讯云代金券
Copyright © 2013 - 2025 Tencent Cloud. All Rights Reserved. 腾讯云 版权所有
深圳市腾讯计算机系统有限公司 ICP备案/许可证号:粤B2-20090059 深公网安备号 44030502008569
腾讯云计算(北京)有限责任公司 京ICP证150476号 | 京ICP备11018762号 | 京公网安备号11010802020287
Copyright © 2013 - 2025 Tencent Cloud.
All Rights Reserved. 腾讯云 版权所有