 |
Cheat Engine The Official Site of Cheat Engine
|
| View previous topic :: View next topic |
| Author |
Message |
The0neThe0nly Expert Cheater
Reputation: 0
Joined: 08 Dec 2009 Posts: 119 Location: In a box
|
Posted: Fri Aug 01, 2014 10:25 pm Post subject: [C++] Getting a set of numbers out of a string |
|
|
I have a long string taken from a website using WinINet. There are some numbers surrounded by parentheses. These numbers change in length occasionally. I can use std::string::find to get the index of the first parentheses, but I've been having troubles getting the 2nd one right after it. And even then, I'm not sure if I am using std::string::substr right.
The snippet of the page code that has the numbers looks like this:
CreateLocale(5918239)
I want my code to return just 5918239 as int.
Right now, my code looks something like this:
"lol" is an std::string of the page source
| Code: | std::string lookingfor = "CreateLocale(";
std::string lookingfor2 = ")";
if (lol.find(lookingfor) == std::string::npos) {
printf("FATAL ERROR: Site may be undergoing maintenance right now\n");
system("pause");
return 0;
}
else if (lol.find(lookingfor2, (lol.find(lookingfor) + strlen("CreateLocale("))) == std::string::npos) {
printf("FATAL ERROR: Unspecified error\n");
system("pause");
return 0;
}
printf("Found it 1! Index: %d\n", lol.find(lookingfor));
printf("Found it 2! Index: %d\n", lol.find(lookingfor2, (lol.find(lookingfor) + strlen("CreateLocale("))));
std::string tempstr = (lol.substr((lol.find(lookingfor) + strlen("CreateLocale("))));
printf("Value 1: %s\n", tempstr); //lol.find(lookingfor2, (lol.find(lookingfor) + strlen("CreateLocale(")))));
system("pause");
return 0; |
Any ideas?
|
|
| Back to top |
|
 |
atom0s Moderator
Reputation: 205
Joined: 25 Jan 2006 Posts: 8587 Location: 127.0.0.1
|
Posted: Sat Aug 02, 2014 2:01 am Post subject: |
|
|
You could do it like this:
| Code: | std::string get_number(const std::string& str)
{
const std::string find_start = "CreateLocale(";
const std::string find_end = ")";
auto start = str.find(find_start, 0);
if (start == str.npos)
return "";
auto end = str.find(find_end, start);
if (start == str.npos)
return "";
start += find_start.size();
return str.substr(start, end - start);
} |
Testing like:
| Code: | std::string test1 = "CreateLocale(5918239)";
std::string test2 = "CreateLocale(59)";
std::string test3 = "CreateLocale(51532643535219)";
std::cout << get_number(test1) << std::endl;
std::cout << get_number(test2) << std::endl;
std::cout << get_number(test3) << std::endl; |
Results with:
5918239
59
51532643535219
Another method you do is with the new C++11 regex features:
| Code: |
#include <regex>
std::string get_number2(const std::string& str)
{
// Attempt to locate the given regular expression..
std::regex find_regex("CreateLocale\\(([0-9]+)\\)");
auto match_count(std::distance(std::sregex_iterator(str.begin(), str.end(), find_regex), std::sregex_iterator()));
// Validate we have a match..
if (match_count == 0)
return "";
// Create the result by using the matching group..
std::string result;
std::regex_replace(std::back_inserter(result), str.begin(), str.end(), find_regex, std::string("$1"));
return result;
} |
_________________
- Retired. |
|
| Back to top |
|
 |
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum You cannot attach files in this forum You can download files in this forum
|
|