Моника обнови решението на 30.10.2015 22:30 (преди над 2 години)
+package main
+
+import (
+ "regexp"
+ "strings"
+)
+
+const (
+ date string = "20\\d{2}(-|\\/)((0[1-9])|(1[0-2]))(-|\\/)((0[1-9])|" +
+ "([1-2][0-9])|(3[0-1]))(T|\\s)(([0-1][0-9])|(2[0-3])):" +
+ "([0-5][0-9]):([0-5][0-9])"
+ ipv4 string = "(25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[0-9]{1,2})(\\.(25[0-5]" +
+ "|2[0-4][0-9]|1[0-9][0-9]|[0-9]{1,2})){3}"
+ newLine string = "\n"
+ emptyString string = ""
+)
+
+func ExtractDate(logContents string) string {
+ re := regexp.MustCompile(date)
+ return strings.Join(re.FindAllString(logContents, -1), newLine) + newLine
+}
+
+func ExtractIPv4(logContents string) string {
+ re := regexp.MustCompile(ipv4)
+ return strings.Join(re.FindAllString(logContents, -1), newLine) + newLine
+}
+
+func ExtractString(logContents string) string {
+ reDate := regexp.MustCompile(date + "\\s")
+ reIPv4 := regexp.MustCompile(ipv4 + "\\s")
+ str := reDate.ReplaceAllString(logContents, emptyString)
+ return reIPv4.ReplaceAllString(str, emptyString)
+}
+
+func ExtractColumn(logContents string, column uint8) string {
+ if len(logContents) == 0 || logContents == newLine {
+ return emptyString
+ }
+
+ switch column {
+ case 0:
+ return ExtractDate(logContents)
+ case 1:
+ return ExtractIPv4(logContents)
+ case 2:
+ return ExtractString(logContents)
+ }
+
+ return emptyString
+}