iterate through something a save it onto an array?


I have a var that holds the following information [[code=666]],[[code=777]], I want to iterate trough those and save "666" and "777" in an array like {"666","777"}. I tried to the the following but it is saving it as {"6","6, "6", "7", "7", "7"}.

[code]
var t = this;
var regex = /\[\[code=.*?\]\]/gi;
var match = t;
match = t.data.u_pricing.match(regex);

var final_code = [];

if(match != null){
//itereate through the matches eg. [[code=666]] [[code=777]]
for( var i=0; i <match.length; i++){
var init_index = match[i].indexOf("=")+1;
var end_index = match[i].length-2;
for( init_index; init_index <end_index; init_index++){
final_code.push(match[i][init_index]);
}
console.log("FINAL CODE" + final_code);
}
}

[/code]
@claudilla
You are definitely not working with C++. But an attempt to solve this problem of yours can be like this
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
#include <iostream>
#include <string>

using namespace std;

/**
*  @code getNumber(const string &str) @endcode
*  @brief Gets a string, extracts the numbers 
*  @param str Mixed string
*  @return string(number) extracted from mixed string
*/
string getNumber(const string &str);

int main() {
    string stringArray[] = {"[[code = 666]]","[[code = 777]]"};

    int size = (sizeof stringArray / sizeof stringArray[0]);

    string *newStringArray = new string[size];

    // Extract number from stringArray and put them into newStringArray
    for (int i = 0; i < size; i++){
	newStringArray[i] = getNumber(stringArray[i]);
    }
	
    // Display number in newStringArray
    for (int i = 0; i < size; i++){
	cout << newStringArray[i] << endl;
    }

    delete[] newStringArray; // free memory
       
    return 0;
}

string getNumber(const string &str){
    string numbers("0123456789");

    string newString = str.substr(str.find_first_of(numbers)); // gets 666]] from "[[code = 666]]"

    string numString = newString.substr(0, newString.find_first_not_of(numbers)); //extract from 666 upto first non-digit
    
    return numString;
}


Hope this helps {some how}
In C++ could use std::sregex_token_iterator to iterate through the std::string to extract the numbers:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include <iostream>
#include <string>
#include <regex>
#include <vector>
#include <sstream>
#include <algorithm>
#include <iterator>

int main()
{
    std::string myString = "[[code=666]],[[code=777]],[[code=888]]";
    std::regex re{"(\\d+)"};
    std::vector<int> numbers{};
    std::sregex_token_iterator iter(myString.cbegin(), myString.cend(), re, 0);
    std::sregex_token_iterator last{};
    for ( ; iter != last; ++iter)
    {
       std::istringstream stream{*iter};
       while(stream)
       std::copy(std::istream_iterator<int>(stream), {}, std::back_inserter(numbers));
    }
    for (const auto & elem : numbers)std::cout << elem << '\n';
}

ps: It seems you're writing C# so have to find something on these lines
Registered users can post here. Sign in or register to post.