Finding a string, in a string, with regex, regex_search

Viewed 221

I have a string:

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 Games\League of Legends" "--app-name=LeagueClient" "--ux-name=LeagueClientUx" "--ux-helper-name=LeagueClientUxHelper" "--log-dir=LeagueClient Logs" "--crash-reporting=crashpad" "--crash-environment=EUW1" "--crash-pipe=\\.\pipe\crashpad_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 Games\League of Legends" "--no-proxy-server";

I wanna grab the port number and remote auth token, and I do that with the following code:

#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 Games\League of Legends" "--app-name=LeagueClient" "--ux-name=LeagueClientUx" "--ux-helper-name=LeagueClientUxHelper" "--log-dir=LeagueClient Logs" "--crash-reporting=crashpad" "--crash-environment=EUW1" "--crash-pipe=\\.\pipe\crashpad_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 Games\League 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);
        








}

´

Gives me the following output:

    53201
remoting-auth-token=13bHJUl7M_u_CtoR7v8XeA--respawn-comman

The amount of characters in port number(53201) doesn't change, so I get that sucessfully. However the remoting-auth-token changes therefore I don't know how I can get it successfully also when changing length.

I wanna grab this part from the remoting auth token: "13bHJUl7M_u_CtoR7v8XeA", so I can store it in a variable for use in my APP, just like I've done with the port number.

Looking forward to hearing from you! :)

2 Answers

You should study the syntax of your expected matches to extract them correctly.

To get the port number value, I'd use

regex reg("--riotclient-app-port=(\\d+)");

This way, you do not even need to care about the number of digits you match since it will capture a number after a known string.

If the auth token can only contain letters, digits, _ or - you may use

regex reg1("remoting-auth-token=([\\w-]+)")

where \w matches a letter/digit/_ and - matches a hyphen, + will match one or more occurrences.

See the C++ demo.

First, you need to escape your str value. Every double-quotes (") character must be escaped with (\")

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 Games\League of Legends\" \"--app-name=LeagueClient\" \"--ux-name=LeagueClientUx\" \"--ux-helper-name=LeagueClientUxHelper\" \"--log-dir=LeagueClient Logs\" \"--crash-reporting=crashpad\" \"--crash-environment=EUW1\" \"--crash-pipe=\\.\pipe\crashpad_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 Games\League of Legends\" \"--no-proxy-server";

Second, use this pattern:

(?:--remoting-auth-token=)([^"]*)

You can access match group with index 1.

To test regexp you can use this link: https://regexr.com/58bpb

Related