1

Example:

here is the string: "blablabla123:550:404:487blablabla500:488:474:401blablablabla"

here is what I'm using:

string reg = "(\\d{1,3}):(\\d{1,3}):(\\d{1,3}):(\\d{1,3})";

this obviously doesn't work since it is looking for starting with the number, and I also want to fetch all results, but I don't know how to do that, even though I looked so much for it. :/

I want to have like 2 arrays with:

Array 1: should return [1] = "123"; [2] = "550"; [3] = "404"; [4] = "487";
Array 2: should return [1] = "500"; [2] = "488"; [3] = "474"; [4] = "401";

#include <regex>
#include <iostream>
#include <string>

#include <conio.h>
using namespace std;

typedef std::tr1::match_results<std::string::const_iterator> str_iterator;
int main () {
    str_iterator res;

string regex = "(\\d{1,3}):(\\d{1,3}):(\\d{1,3}):(\\d{1,3})";
string str = "blablabla123:550:404:487blablabla500:488:474:401blablablabla";
const std::tr1::regex pattern(regex.c_str());
bool valid = std::tr1::regex_match(str, res, pattern);
//res is gonna be an array with the match results
if(valid){
    printf("Matched with size of %d\n", res.size());
    printf("Result 1: %s",res[1]);
    printf("Result 2: %s",res[2]);
    printf("Result 3: %s",res[3]);
    printf("Result 4: %s",res[4]);
}else{
    printf("Not Matched");
}
_getch();
return 0;
}

2 Answers 2

1

regex_match will try to match the entire string. Obviously in your case, the result will be false (it won't match).

Try to use regex_search instead.

Sign up to request clarification or add additional context in comments.

1 Comment

Yea that sound about right,I hope you had explained how could I use regex_search to do that, but since you didn't , it shoud be another topic, so here it goes, I hope you can answer: stackoverflow.com/questions/9823367/…
-1

What about this :

[^\d]*(\d+)[^\d]+(\d+)[^\d]+(\d+)[^\d]+(\d+)[^\d]+(\d+)[^\d]+(\d+)[^\d]+(\d+)[^\d]+(\d+)[^\d]*

It's awful but we'll retrieve 8 numbers separated by any other characters.

group(0)    group(1)    group(2)    group(3)    group(4)    group(5)    group(6)    group(7)    
123           550         404         487         500         488           474         401

Else you could modify the String replacing every non digit by : which would give you: :::::::::123:550:404:487:::::::::500:488:474:401:::::::::::: You just keep non empty values by parsing the String. But that would only work if you're certain not to have empty values

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.