2016-04-09 4 views
0

Ich habe ein Byte-Array, das die Ausgabe eines Active Directory-Aufrufs enthält. Ich möchte dies analysieren und die Anzahl der Tage extrahieren, bis mein Konto abläuft. Jetzt frage ich mich: Was ist der beste Weg, um 22-4-2016 11:05:26 (so der Wert nach Password Expires) zu extrahieren?Los, extrahiere Tage aus Byte-Array

[]byte(`The request will be processed at a domain controller for domain local.nl.bol.com. 

User name     bla 
Full Name     bla bla 
Comment 
User's comment 
Country code     (null) 
Account active    Yes 
Account expires    Never 

Password last set   13-3-2016 11:05:26 
Password expires    22-4-2016 11:05:26 
Password changeable   13-3-2016 11:05:26 
Password required   Yes 
User may change password  Yes 

Workstations allowed   All 
Logon script     bla.bat 
User profile 
Home directory 
Last logon     31-3-2016 7:59:29 

Logon hours allowed   All 

The command completed successfully.`) 

Antwort

1

Mit strings.TrimSpace, strings.Index und unter Bezugnahme auf ähnliche Stackoverflow Antworten, habe ich eine Arbeitslösung und bitte finden Code arbeiten unter: -

package main 

import (
    "fmt" 
    "strings" 
) 

func CToGoString(c []byte) string { 
    n := -1 
    for i, b := range c { 
     if b == 0 { 
      break 
     } 
     n = i 
    } 
    return string(c[:n+1]) 
} 

func main() { 

    s := []byte(`The request will be processed at a domain controller for domain local.nl.bol.com. 

User name     bla 
Full Name     bla bla 
Comment 
User's comment 
Country code     (null) 
Account active    Yes 
Account expires    Never 

Password last set   13-3-2016 11:05:26 
Password expires    22-4-2016 11:05:26 
Password changeable   13-3-2016 11:05:26 
Password required   Yes 
User may change password  Yes 

Workstations allowed   All 
Logon script     bla.bat 
User profile 
Home directory 
Last logon     31-3-2016 7:59:29 

Logon hours allowed   All 

The command completed successfully.`) 

d := CToGoString(s) 
    len := len("Password expires") 
i := strings.Index(d, "Password expires") 
j := strings.Index(d, "Password changeable") 
chars := d[i+len:j] 
fmt.Println(strings.TrimSpace(chars)) 
} 

Haben Code zum Spielplatz posted: http://play.golang.org/p/t0Xjd04-pi

1

Sie Sie können dies tun, indem Sie das [] Byte in eine Zeichenkette konvertieren und dann das Strings-Paket verwenden, um den Wert zu finden und zu extrahieren und ihn schließlich mit der Zeit zu analysieren.Parsen Sie die Zeichenkette in eine Zeit um, die Sie bearbeiten können mit.

package main 

import (
    "fmt" 
    "strings" 
    "time" 
    "log" 
) 

func main() { 
    line := data[strings.Index(data, "Password expires"):strings.Index(data, "Password changeable")] 
    date := strings.TrimSpace(strings.TrimPrefix(line, "Password expires")) 
    fmt.Println(date) 
    pDate, err := time.Parse("02-1-2006 03:04:05", date) 
    if err != nil { 
     log.Fatal(err) 
    } 
    fmt.Println(pDate) 
} 

var data = string([]byte(`The request will be processed at a domain controller for domain local.nl.bol.com. 

User name     bla 
Full Name     bla bla 
Comment 
User's comment 
Country code     (null) 
Account active    Yes 
Account expires    Never 

Password last set   13-3-2016 11:05:26 
Password expires    22-4-2016 11:05:26 
Password changeable   13-3-2016 11:05:26 
Password required   Yes 
User may change password  Yes 

Workstations allowed   All 
Logon script     bla.bat 
User profile 
Home directory 
Last logon     31-3-2016 7:59:29 

Logon hours allowed   All 

The command completed successfully.`)) 

Auf der playground.