0
String st = "64.242.88.10 - - [07/Mar/2004:16:06:51 -0800] "GET /twiki/bin/rdiff/TWiki/NewUserTemplate?rev1=1.3&rev2=1.2 HTTP/1.1" 200 4523"
String ip, url;
int index = line.indexOf(" - - ");
ip = line.substring(0, index)

this will extract the ip and i need to extract the link which is after GET into two different variable, i extract the ip without using regx but i could not to have the link.

deHaar
  • 17,687
  • 10
  • 38
  • 51
Saeed ALSferi
  • 57
  • 1
  • 1
  • 6

2 Answers2

2

You can split() the String by an arbitrary amount of whitespaces and take the first item of the result:

public static void main(String[] args) {
    String st = "64.242.88.10 - - [07/Mar/2004:16:06:51 -0800] \"GET /twiki/bin/rdiff/TWiki/NewUserTemplate?rev1=1.3&rev2=1.2 HTTP/1.1\" 200 4523";
    // in split, use a regular expression for an arbitrary amount of whitespaces
    String[] splitResult = st.split("\\s+");
    // take the first item from the resulting array
    String ip = splitResult[0];
    // and print it
    System.out.println(ip);
}

Your String has to be a valid one, then this will work...

The output is just

64.242.88.10
deHaar
  • 17,687
  • 10
  • 38
  • 51
0

Question should be enlightened, following code shows how regex can be used to extract IP and GET parameters.

String st = "64.242.88.10 - - [07/Mar/2004:16:06:51 -0800] \"GET /twiki/bin/rdiff/TWiki/NewUserTemplate?rev1=1.3&rev2=1.2 HTTP/1.1\" 200 4523";
Pattern pat = Pattern.compile( "(\\d+\\.\\d+\\.\\d+\\.\\d+)(?:(?!GET).)*GET ([^ ]*)" );
Matcher mat = pat.matcher( st );
while ( mat.find() ) {
    System.out.println( "1: " + mat.group( 1 ) + "; 2: " + mat.group( 2 ) );
}
Nahuel Fouilleul
  • 18,726
  • 2
  • 31
  • 36
  • When going for regexes, be prepared for the real regex IP address magic, see https://stackoverflow.com/questions/15875013/extract-ip-addresses-from-strings-using-regex for example . At least theoretically ;-) – GhostCat Nov 06 '19 at 13:30