Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on Jun 21st, 2012  |  syntax: None  |  size: 2.17 KB  |  hits: 20  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. Parsing Jetty log records
  2. 70.80.110.200 -  -  [12/Apr/2011:05:47:34 +0000] "GET /notify/click?r=http://www.xxxxxx.com/hello_world&rt=1302587231462&iid=00000 HTTP/1.1" 302 0 "-" "Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.0; Trident/4.0; FunWebProducts; HotbarSearchToolbar 1.1; SLCC1; .NET CLR 2.0.50727; Media Center PC 5.0; .NET CLR 3.5.30729; .NET CLR 3.0.30729; .NET4.0C; AskTbFWV5/5.11.3.15590)" 4 4
  3.        
  4. import java.util.*;
  5. import java.util.regex.*;
  6.  
  7. public class RegexDemo {
  8.  
  9.   public static void main( String[] argv ) {
  10.     String pat = "^([0-9.]*).*?\[(\d+\/\w+\/\d+):(\d+:\d+:\d+).*?\].*?(\/[^ ]*).*$";
  11.     Pattern p = Pattern.compile(pat);
  12.     String target = "70.80.110.200 -  -  [12/Apr/2011:05:47:34 +0000] "GET /notify/click?r=http://www.xxxxxx.com/hello_world&rt=1302587231462&iid=00000 HTTP/1.1" 302 0 "-" "Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.0; Trident/4.0; FunWebProducts; HotbarSearchToolbar 1.1; SLCC1; .NET CLR 2.0.50727; Media Center PC 5.0; .NET CLR 3.5.30729; .NET CLR 3.0.30729; .NET4.0C; AskTbFWV5/5.11.3.15590)" 4 4";
  13.     Matcher m = p.matcher(target);
  14.     System.out.println("pattern: " + pat);
  15.     System.out.println("target: " + target);
  16.  
  17.     if (m.matches()) {
  18.       System.out.println("found");
  19.       for (int i=0; i <= m.groupCount(); ++i) {
  20.         System.out.println(m.group(i));
  21.       }
  22.     }
  23.   }
  24. }
  25.        
  26. /^([0-9.]+).*?[(d+/w+/d+):(d+:d+:d+).*?].*?(/[^ ]*).*$/
  27.        
  28. String s = "70.80.110.200 -  -  [12/Apr/2011:05:47:34 +0000] "GET /notify/click?r=http://www.xxxxxx.com/hello_world&rt=1302587231462&iid=00000 HTTP/1.1" 302 0 "-" "Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.0; Trident/4.0; FunWebProducts; HotbarSearchToolbar 1.1; SLCC1; .NET CLR 2.0.50727; Media Center PC 5.0; .NET CLR 3.5.30729; .NET CLR 3.0.30729; .NET4.0C; AskTbFWV5/5.11.3.15590)" 4 4";
  29. Pattern p = Pattern.compile("^(\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}).*?\" + //ip
  30.                             "[([^:]*):"+ //date
  31.                             "(\d{2}:\d{2}:\d{2}).*?\].*?"+ //time
  32.                             "(/[^\s]*).*$"); //uri
  33.  
  34. Matcher m = p.matcher(s);
  35. if(m.find()){
  36.     String ip = m.group(1);
  37.     String date = m.group(2);
  38.     String time = m.group(3);
  39.     String uri = m.group(4);
  40. }