英文:
Go, extract days out of byte array
问题
我有一个包含Active Directory调用输出的字节数组。我想解析它并提取我的帐户到期的天数。现在我想知道:提取22-4-2016 11:05:26
(即Password Expires
之后的值)的最佳方法是什么?
[]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.`)
英文:
I have a byte array containing the output of an Active Directory call. I want to parse this and extract the amount of days until my account expires. Now I'm wondering: what's the best way to extract 22-4-2016 11:05:26
(so the value after Password Expires
)?
[]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.`)
答案1
得分: 1
使用strings.TrimSpace
、strings.Index
并参考相关的stackoverflow答案,我得到了一个可行的解决方案,请查看下面的工作代码:
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))
}
已将代码发布到playground:http://play.golang.org/p/t0Xjd04-pi
英文:
Using strings.TrimSpace
, strings.Index
and referring to related stackoverflow answers, I got a working solution and please find working code below:-
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))
}
Have posted code to playground: http://play.golang.org/p/t0Xjd04-pi
答案2
得分: 1
你可以通过将[]byte转换为字符串,然后使用strings包来查找和提取值,最后使用time.Parse将字符串解析为可操作的时间。
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.`))
在playground上可以运行。
英文:
You can do it by converting the []byte into a string and then using the strings package to find and extract the value and finally parsing it with time.Parse to convert the string to a time that you can work with.
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.`))
On the playground.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论