I'm trying to read from a text file that contains search information: IP address, time of search, search words, and the link clicked on. I'm trying to traverse the file line by line and extract just the IP address on each one, and I'm doing this by adding each character to a String until I hit the first space of that line. However, as you can see in the screenshot, there is a much larger gap between IP address and the time than there is between sea-level and pressure. This gap isn't being registered as a space, and the first space that the program is finding in the whole line is the one between sea-level and pressure. Because of this I'm not able to extract just the IP. Is there a way to work around this or fix it?
Asked
Active
Viewed 265 times
-1
-
Are the gaps tabs? Search for "\t" – Tim Sep 10 '16 at 15:28
-
what happens with delimiter `\s+` ? – Saravana Sep 10 '16 at 15:31
-
follow http://stackoverflow.com/help/mcve – Pavneet_Singh Sep 10 '16 at 15:33
-
@Tim That worked, thank you so much! – Akhil Pothana Sep 10 '16 at 15:38
-
Added as an answer below. Please accept, thanks! – Tim Sep 10 '16 at 15:44
3 Answers
0
Depending on your language, a '/\s+/'
should do the trick.

Community
- 1
- 1

tristansokol
- 4,054
- 2
- 17
- 32
0
- You could use a regex to extract ip address, extract each line into a string then you could use something like.
string line = extract_line();
String ip_pattern = "^(?:[0-9]{1,3}.){3}[0-9]{1,3}$";
Pattern r = Pattern.compile(ip_patter);
Matcher m = r.matcher(line);
m.group(0);
has the ip address if it finds any, else it would be null.
If more than one ip address are present in the line you can index them m.group(n).
Derived with the help of following links:

sai
- 434
- 5
- 13