Last active
September 30, 2019 05:53
-
-
Save lxfly2000/957638796499d943a94d50a44e05bf42 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include<iostream> | |
#include<string> | |
#include<regex> | |
//查找并返回串中的第一个子串(小括号里的),若未找到则返回空字符串 | |
std::string regex_match1(const std::string& src, const std::string& reg_str) | |
{ | |
std::regex r(reg_str); | |
std::smatch m; | |
if (!std::regex_search(src, m, r)) | |
return std::string();//默认构造函数会产生一个空字符串 | |
return m[1];//0表示搜索串搜索到的整个结果,从1开始才是小括号中的分组子串 | |
} | |
//查找并返回串中的第一个子串(小括号里的),若未找到则返回空字符串 | |
std::wstring wregex_match1(const std::wstring& src, const std::wstring& reg_str) | |
{ | |
std::wregex r(reg_str); | |
std::wsmatch m; | |
if (!std::regex_search(src, m, r)) | |
return std::wstring(); | |
return m[1]; | |
} | |
int main() | |
{ | |
std::string uri = "https://lxfly2000.github.io/assets/cover/ac5087850_ac239.png"; | |
std::cout << uri << " >>> " << regex_match1(uri, "ac(\\d+)") << std::endl; | |
std::cout << std::regex_replace(uri, std::regex("ac\\d+"), "ac666666",std::regex_constants::format_first_only);//替换后的结果在返回值中,原字符串不会被修改,format_first_only表示只替换第一个匹配项,默认为全部替换 | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment