去,从字节数组中提取天数。

huangapple go评论139阅读模式
英文:

Go, extract days out of byte array

问题

我有一个包含Active Directory调用输出的字节数组。我想解析它并提取我的帐户到期的天数。现在我想知道:提取22-4-2016 11:05:26(即Password Expires之后的值)的最佳方法是什么?

  1. []byte(`The request will be processed at a domain controller for domain local.nl.bol.com.
  2. User name bla
  3. Full Name bla bla
  4. Comment
  5. User's comment
  6. Country code (null)
  7. Account active Yes
  8. Account expires Never
  9. Password last set 13-3-2016 11:05:26
  10. Password expires 22-4-2016 11:05:26
  11. Password changeable 13-3-2016 11:05:26
  12. Password required Yes
  13. User may change password Yes
  14. Workstations allowed All
  15. Logon script bla.bat
  16. User profile
  17. Home directory
  18. Last logon 31-3-2016 7:59:29
  19. Logon hours allowed All
  20. 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)?

  1. []byte(`The request will be processed at a domain controller for domain local.nl.bol.com.
  2. User name bla
  3. Full Name bla bla
  4. Comment
  5. User's comment
  6. Country code (null)
  7. Account active Yes
  8. Account expires Never
  9. Password last set 13-3-2016 11:05:26
  10. Password expires 22-4-2016 11:05:26
  11. Password changeable 13-3-2016 11:05:26
  12. Password required Yes
  13. User may change password Yes
  14. Workstations allowed All
  15. Logon script bla.bat
  16. User profile
  17. Home directory
  18. Last logon 31-3-2016 7:59:29
  19. Logon hours allowed All
  20. The command completed successfully.`)

答案1

得分: 1

使用strings.TrimSpacestrings.Index并参考相关的stackoverflow答案,我得到了一个可行的解决方案,请查看下面的工作代码:

  1. package main
  2. import (
  3. "fmt"
  4. "strings"
  5. )
  6. func CToGoString(c []byte) string {
  7. n := -1
  8. for i, b := range c {
  9. if b == 0 {
  10. break
  11. }
  12. n = i
  13. }
  14. return string(c[:n+1])
  15. }
  16. func main() {
  17. s := []byte(`The request will be processed at a domain controller for domain local.nl.bol.com.
  18. User name bla
  19. Full Name bla bla
  20. Comment
  21. User's comment
  22. Country code (null)
  23. Account active Yes
  24. Account expires Never
  25. Password last set 13-3-2016 11:05:26
  26. Password expires 22-4-2016 11:05:26
  27. Password changeable 13-3-2016 11:05:26
  28. Password required Yes
  29. User may change password Yes
  30. Workstations allowed All
  31. Logon script bla.bat
  32. User profile
  33. Home directory
  34. Last logon 31-3-2016 7:59:29
  35. Logon hours allowed All
  36. The command completed successfully.`)
  37. d := CToGoString(s)
  38. len := len("Password expires")
  39. i := strings.Index(d, "Password expires")
  40. j := strings.Index(d, "Password changeable")
  41. chars := d[i+len:j]
  42. fmt.Println(strings.TrimSpace(chars))
  43. }

已将代码发布到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:-

  1. package main
  2. import (
  3. "fmt"
  4. "strings"
  5. )
  6. func CToGoString(c []byte) string {
  7. n := -1
  8. for i, b := range c {
  9. if b == 0 {
  10. break
  11. }
  12. n = i
  13. }
  14. return string(c[:n+1])
  15. }
  16. func main() {
  17. s := []byte(`The request will be processed at a domain controller for domain local.nl.bol.com.
  18. User name bla
  19. Full Name bla bla
  20. Comment
  21. User's comment
  22. Country code (null)
  23. Account active Yes
  24. Account expires Never
  25. Password last set 13-3-2016 11:05:26
  26. Password expires 22-4-2016 11:05:26
  27. Password changeable 13-3-2016 11:05:26
  28. Password required Yes
  29. User may change password Yes
  30. Workstations allowed All
  31. Logon script bla.bat
  32. User profile
  33. Home directory
  34. Last logon 31-3-2016 7:59:29
  35. Logon hours allowed All
  36. The command completed successfully.`)
  37. d := CToGoString(s)
  38. len := len("Password expires")
  39. i := strings.Index(d, "Password expires")
  40. j := strings.Index(d, "Password changeable")
  41. chars := d[i+len:j]
  42. fmt.Println(strings.TrimSpace(chars))
  43. }

Have posted code to playground: http://play.golang.org/p/t0Xjd04-pi

答案2

得分: 1

你可以通过将[]byte转换为字符串,然后使用strings包来查找和提取值,最后使用time.Parse将字符串解析为可操作的时间。

  1. package main
  2. import (
  3. "fmt"
  4. "strings"
  5. "time"
  6. "log"
  7. )
  8. func main() {
  9. line := data[strings.Index(data, "Password expires"):strings.Index(data, "Password changeable")]
  10. date := strings.TrimSpace(strings.TrimPrefix(line, "Password expires"))
  11. fmt.Println(date)
  12. pDate, err := time.Parse("02-1-2006 03:04:05", date)
  13. if err != nil {
  14. log.Fatal(err)
  15. }
  16. fmt.Println(pDate)
  17. }
  18. var data = string([]byte(`The request will be processed at a domain controller for domain local.nl.bol.com.
  19. User name bla
  20. Full Name bla bla
  21. Comment
  22. User's comment
  23. Country code (null)
  24. Account active Yes
  25. Account expires Never
  26. Password last set 13-3-2016 11:05:26
  27. Password expires 22-4-2016 11:05:26
  28. Password changeable 13-3-2016 11:05:26
  29. Password required Yes
  30. User may change password Yes
  31. Workstations allowed All
  32. Logon script bla.bat
  33. User profile
  34. Home directory
  35. Last logon 31-3-2016 7:59:29
  36. Logon hours allowed All
  37. 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.

  1. package main
  2. import (
  3. "fmt"
  4. "strings"
  5. "time"
  6. "log"
  7. )
  8. func main() {
  9. line := data[strings.Index(data, "Password expires"):strings.Index(data, "Password changeable")]
  10. date := strings.TrimSpace(strings.TrimPrefix(line, "Password expires"))
  11. fmt.Println(date)
  12. pDate, err := time.Parse("02-1-2006 03:04:05", date)
  13. if err != nil {
  14. log.Fatal(err)
  15. }
  16. fmt.Println(pDate)
  17. }
  18. var data = string([]byte(`The request will be processed at a domain controller for domain local.nl.bol.com.
  19. User name bla
  20. Full Name bla bla
  21. Comment
  22. User's comment
  23. Country code (null)
  24. Account active Yes
  25. Account expires Never
  26. Password last set 13-3-2016 11:05:26
  27. Password expires 22-4-2016 11:05:26
  28. Password changeable 13-3-2016 11:05:26
  29. Password required Yes
  30. User may change password Yes
  31. Workstations allowed All
  32. Logon script bla.bat
  33. User profile
  34. Home directory
  35. Last logon 31-3-2016 7:59:29
  36. Logon hours allowed All
  37. The command completed successfully.`))

On the playground.

huangapple
  • 本文由 发表于 2016年4月9日 20:12:36
  • 转载请务必保留本文链接:https://go.coder-hub.com/36516592.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定