Reading past whitespace in a String

Hello All,

I am trying to read IP and Port from a string with the format IP : Port. But I cant figure out how to read past the white spaces after the IP and before the port... Here is my code below...

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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
  std::vector<std::string> extractIP( std::string data )
{
	std::vector<std::string> results;
	//start at the beginning of the string
	int pos = 0;
	int wordstart=0;
	int s = data.size();

	//loop though characters
	while(pos < s) {
		//if valid ascii and not space this could be the first part
		if( ( data[pos] < 48 || data[pos] > 57 || pos+1 >= s) && data[pos] != '.' && data[pos] != ':' ) { //whitespace or ,:| seperators or reached end

			//test if word length >= 7 for minimal ip 1.1.1.1
			if(pos-wordstart >= 7) {
				//create a string to hold this word
				std::string word = data.substr(wordstart, pos-wordstart);
				std::cout << "Word: " << word << std::endl;
				//test if has an . in it
				size_t firstfound = word.find('.');

				//see if found
				if( firstfound != std::string::npos ) {
					//lets check if theres another . after the first .
					size_t secondfound = word.find('.', firstfound+1);

					if( secondfound != std::string::npos ) {
						//lets check if theres another . after the second .
						size_t thirdfound = word.find('.', secondfound+1);

						if( thirdfound != std::string::npos ) {
							//check to make sure . isnt at end
							if(thirdfound < word.size()-1) {
								// Make sure that there isn't a . at the end
								cout<<"IP FOUND..."<<std::endl;
								size_t fourthfound = word.find('.', thirdfound+1);

								if( fourthfound == std::string::npos ) {
									//Finds the first space and skips it
									size_t space = word.find(" ");

									while(space != std::string::npos ) {
										pos+1;
									}

									cout<<"space"<<std::endl;

									// checks total word size
									if(word.size()<=26) {
										//Finds the :
										size_t fifthfound = word.find(':', thirdfound+1);

										if( fifthfound != std::string::npos ) {
											if(fifthfound < word.size()-1) {
												fifthfound = word.rfind(':');

												if(fifthfound < word.size()-1) {
													//Finds and skips the second space
													size_t spaces = word.find(" ");

													while(spaces != std::string::npos ) {
														pos+1;
													}

													cout<<"space 2"<<std::endl;

													// Port Finding
													if( word.size() - fifthfound<=7) {
														//Keeps all numbers in check
														if(firstfound<4 && secondfound-firstfound<=4 && thirdfound-secondfound<=4 && fifthfound-thirdfound<=5) {
															if(firstfound>0 && secondfound-firstfound>1 && thirdfound-secondfound>1 && fifthfound-thirdfound>1) {
																cout<<"porting"<<std::endl;
																//add to vector
																results.push_back(word);
															}
														}
													}
												}
											}
										}
									}
								}
							}
						}
					}
				}
			}

			wordstart = pos+1; // this pos + 1 so its after the whitespace
		}

		//goto the next position
		pos++;
	}

	return results;
}

int main()
{
	std::cout << "C++ IP Extractor" << std::endl;
	// 3 right followed by different wrong cases ending in 1 right
	std::string data = "74.197.153.238 : 123456 78.197.755.987: 2.2.2.2:9876 ip:address:not:right not.right.ip.address 5..5.123 1.1.1 2.2 12345... 1.2..3.4 5.4.3..2 321.4567.321.5 5.75.5. 12345.6.4.2 ...4567 ..57.66 1.1.1.1:1234567";
	data += ' ';
	std::vector<std::string> results = extractIP(data);
	int i = 0;
	int s = results.size();

	while(i < s) {
		std::cout << "Found " << i << ": " << results[i] << std::endl;
		++i;
	}
}



I can get it to read all of it without the spaces, but I would like for it to include the spaces.

Thanks
Last edited on
Topic archived. No new replies allowed.