使用正则表达式regex_search在字符串中查找字符串
Finding a string, in a string, with regex, regex_search
我有一个字符串:
string str = "C:/Riot Games/League of Legends/LeagueClientUx.exe" "--riotclient-auth-token=yvjM3_sRqdaFoETdKSt1bQ" "--riotclient-app-port=53201" "--no-rads" "--disable-self-update" "--region=EUW" "--locale=en_GB" "--remoting-auth-token=13bHJUl7M_u_CtoR7v8XeA" "--respawn-command=LeagueClient.exe" "--respawn-display-name=League of Legends" "--app-port=53230" "--install-directory=C:Riot GamesLeague of Legends" "--app-name=LeagueClient" "--ux-name=LeagueClientUx" "--ux-helper-name=LeagueClientUxHelper" "--log-dir=LeagueClient Logs" "--crash-reporting=crashpad" "--crash-environment=EUW1" "--crash-pipe=\.pipecrashpad_12076_CFZRMYHTBJGPBIUH" "--app-log-file-path=C:/Riot Games/League of Legends/Logs/LeagueClient Logs/2020-07-13T13-33-41_12076_LeagueClient.log" "--app-pid=12076" "--output-base-dir=C:Riot GamesLeague of Legends" "--no-proxy-server";
我想获取端口号和远程身份验证令牌,并使用以下代码完成:
#include <regex>
#include <iostream>
#include <string>
#include <Windows.h>
using namespace std;
string PrintMatch(std::string str, std::regex reg) {
smatch matches;
while (regex_search(str,matches,reg))
{
cout << matches.str(1) << endl;
break;
}
return matches.str(1);
}
int main() {
string str = "C:/Riot Games/League of Legends/LeagueClientUx.exe" "--riotclient-auth-token=yvjM3_sRqdaFoETdKSt1bQ" "--riotclient-app-port=53201" "--no-rads" "--disable-self-update" "--region=EUW" "--locale=en_GB" "--remoting-auth-token=13bHJUl7M_u_CtoR7v8XeA" "--respawn-command=LeagueClient.exe" "--respawn-display-name=League of Legends" "--app-port=53230" "--install-directory=C:Riot GamesLeague of Legends" "--app-name=LeagueClient" "--ux-name=LeagueClientUx" "--ux-helper-name=LeagueClientUxHelper" "--log-dir=LeagueClient Logs" "--crash-reporting=crashpad" "--crash-environment=EUW1" "--crash-pipe=\.pipecrashpad_12076_CFZRMYHTBJGPBIUH" "--app-log-file-path=C:/Riot Games/League of Legends/Logs/LeagueClient Logs/2020-07-13T13-33-41_12076_LeagueClient.log" "--app-pid=12076" "--output-base-dir=C:Riot GamesLeague of Legends" "--no-proxy-server";
regex reg("([0-9][0-9][0-9][0-9][0-9])");
string port = PrintMatch(str, reg);
regex reg1("(remoting-auth-token=[^d]*)");
string output = PrintMatch(str, reg1);
}
´
给我以下输出:
53201
remoting-auth-token=13bHJUl7M_u_CtoR7v8XeA--respawn-comman
端口号(53201(中的字符数没有改变,所以我成功地得到了它。然而,远程身份验证令牌会更改,因此我不知道在更改长度时如何成功获得它。
我想从远程授权令牌中获取这一部分:";13bHJUl7M_u_;,所以我可以把它存储在一个变量中,以便在我的应用程序中使用,就像我对端口号所做的那样。
期待您的来信!:(
您应该研究预期匹配的语法以正确提取它们。
要获得端口号值,我会使用
regex reg("--riotclient-app-port=(\d+)");
这样,您甚至不需要关心匹配的位数,因为它会在已知字符串之后捕获一个数字。
如果身份验证令牌只能包含字母、数字、_
或-
,则可以使用
regex reg1("remoting-auth-token=([\w-]+)")
在w
与字母/数字/_
匹配并且-
与连字符匹配的情况下,+
将匹配一个或多个出现。
请参阅C++演示。
首先,您需要转义str值。每个双引号("(字符都必须用(\"(转义
string str = "C:/Riot Games/League of Legends/LeagueClientUx.exe" "--riotclient-auth-token=yvjM3_sRqdaFoETdKSt1bQ" "--riotclient-app-port=53201" "--no-rads" "--disable-self-update" "--region=EUW" "--locale=en_GB" "--remoting-auth-token=13bHJUl7M_u_CtoR7v8XeA" "--respawn-command=LeagueClient.exe" "--respawn-display-name=League of Legends" "--app-port=53230" "--install-directory=C:Riot GamesLeague of Legends" "--app-name=LeagueClient" "--ux-name=LeagueClientUx" "--ux-helper-name=LeagueClientUxHelper" "--log-dir=LeagueClient Logs" "--crash-reporting=crashpad" "--crash-environment=EUW1" "--crash-pipe=\.pipecrashpad_12076_CFZRMYHTBJGPBIUH" "--app-log-file-path=C:/Riot Games/League of Legends/Logs/LeagueClient Logs/2020-07-13T13-33-41_12076_LeagueClient.log" "--app-pid=12076" "--output-base-dir=C:Riot GamesLeague of Legends" "--no-proxy-server";
第二,使用这种模式:
(?:--remoting-auth-token=)([^"]*)
您可以访问索引为1的匹配组。
要测试regexp,可以使用以下链接:https://regexr.com/58bpb
- 使用正则表达式regex_search在字符串中查找字符串
- 按类型与字符串查找对象
- 使用C RTTI(内置)通过字符串查找功能指针
- 关于获取行和字符串查找函数的问题
- 字符串查找方法找不到第一字母
- C++ 字符串.查找()
- 遇到字符串::查找的问题
- 将一个数组作为子字符串查找到另一个数组中
- 字符串查找第一个非的 C++ 问题
- 如何将通配符与字符串::查找一起使用
- 字符串::查找问题 (C++)
- 性能标准::strstr vs. 标准::字符串::查找
- C++字符串::查找崩溃应用程序
- 使用字符串查找单词的正确方法是什么
- 如何在<string>没有 std::string 中介的情况下制作一个支持通过 C 字符串查找的集合?
- 使用C++根据行中的第一个字符串查找行(仅一个)
- Borland字符串::查找bug
- std::map如何通过字符串查找元素
- 字符串查找函数返回奇数
- 如何使用子字符串查找数字行的正确部分