Extracting specific characters from a string.

Hello, can some one please tell me how do I read this file "scanner.lua" and then how do I extract from it specific numeric characters?


File constains this:



scanner = {
["inventory"] = {
{
["info"] = "|cff0070dd|Hitem:38661:0:0:0:0:0:0:0:68|h[Greathelm of the Scourge Champion]|h|r",
["id"] = "38661",
["count"] = 1,
["id2"] = "38661",
["count2"] = 1,
}, -- [1]
{
["info"] = "|cff0070dd|Hitem:38662:0:0:0:0:0:0:0:68|h[Bladed Ebon Amulet]|h|r",
["id"] = "38662",
["count"] = 1,
["id2"] = "38662",
["count2"] = 1,
}, -- [2]

I want to extract the highlighted numeric characters only. And then the process to be repeated for [2]...Can someone please help me through this? Thank you.
Last edited on
Hmmm...maybe if I re-edit my question a byt. Here it goes:
How to extract an alpha-numeric text from a file, then extract only a few characters from that string.
For example: I have this file "x.txt" containing this string "abcdefdg:hksjdkajwi".
And I want to extract from the string of characters only the ones after ":". How do I do that?
Last edited on
You need to get each line
a line can contain one or more Hitem:XXXXX
find that substring
extract number
you can do something like this, but make sure to cover possibilities
hope this helps to get you an idea

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
45
ifstream fileFromRead;
	fileFromRead.open("test.txt");
	if(!fileFromRead.is_open())
	{
		cout << "Problem in openining the file " << endl;
		return 1;
	}
	string line;
	string item;
	while( fileFromRead.good())
	{
		string::size_type pos=0;
		getline(fileFromRead,line);
		cout << line << endl;
		pos=line.find("Hitem:",pos);
		while(pos != string::npos)
		{
			cout << "pos= " << pos << endl;
			item=line.substr(pos,11);// (starting_pos, size)
			cout << item << endl;
			// then parse item string to extract the integer
			// first we will find the position of :
			string::size_type colpos=item.find(":");
			if(colpos != string::npos)
			{
				istringstream itemStream;
				string hitem;
				int number;
				item.replace(colpos,1," ");
				cout << item << endl;
				itemStream.str(item);
				itemStream >> hitem;
				itemStream >> number;
				cout << "hitem= " << hitem << endl;
				cout << "number= " << number << endl;
			}
			

			pos=line.find("Hitem:",pos+1); // find the next one
		}
		//cout << fileFromRead.good() << endl;
	}
	
	fileFromRead.close();
Thank you so much, but now I hit another wall, how do I take the resulted number, and place it in a function. Let's say I got the first HITEM:38661, but before I tell him to go to the next HITEM I want to do a function with the current one. How do I do that?
Last edited on
I didn't get it properly, is this you want
at line 33 you got the number so call your function immediately
1
2
3
itemStream >> number;
YourFunction(number); // this function will do whatever you want
Topic archived. No new replies allowed.