-1

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?

screenshot of data in text file

Akhil Pothana
  • 21
  • 1
  • 4

3 Answers3

0

Depending on your language, a '/\s+/' should do the trick.

See Explode string by one or more spaces or tabs

Community
  • 1
  • 1
tristansokol
  • 4,054
  • 2
  • 17
  • 32
0
  1. 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:

  1. http://www.tutorialspoint.com/java/java_regular_expressions.htm

  2. https://www.safaribooksonline.com/library/view/regular-expressions-cookbook/9780596802837/ch07s16.html

sai
  • 434
  • 5
  • 13
0

The large spaces are tabs. Search for "\t"

Tim
  • 2,089
  • 1
  • 12
  • 21