NoPaste

emailextract.go

von paedubucher
SNIPPET_DESC:
Extraktion von Informationen aus E-Mail-Adressen
SNIPPET_CREATION_TIME:
19.06.2022 17:27:07
SNIPPET_PRUNE_TIME:
Unendlich

SNIPPET_TEXT:
  1. package dfdegoregexp
  2.  
  3. import (
  4.         "fmt"
  5.         "regexp"
  6. )
  7.  
  8. var (
  9.         // TODO: This regexp must be written. Figure out group names according to switch/case below.
  10.         r = ``
  11.         p = regexp.MustCompile(r)
  12. )
  13.  
  14. type emailInfo struct {
  15.         first, last, company string
  16.         year                 int
  17. }
  18.  
  19. func (e emailInfo) String() string {
  20.         if e.first == "" || e.company == "" {
  21.                 return ""
  22.         }
  23.         var y int
  24.         if e.year != 0 {
  25.                 if e.year >= 100 {
  26.                         y = e.year
  27.                 } else {
  28.                         y = 1900 + e.year
  29.                 }
  30.         }
  31.         if e.last != "" && y != 0 {
  32.                 return fmt.Sprintf("%s %s, *%d, %s", e.first, e.last, y, e.company)
  33.         }
  34.         if e.last != "" && y == 0 {
  35.                 return fmt.Sprintf("%s %s, %s", e.first, e.last, e.company)
  36.         }
  37.         if e.last == "" && y == 0 {
  38.                 return fmt.Sprintf("%s, %s", e.first, e.company)
  39.         }
  40.         return ""
  41. }
  42.  
  43. func Extract(email string) string {
  44.         matches := p.FindStringSubmatch(email)
  45.         if len(matches) == 0 {
  46.                 return ""
  47.         }
  48.         var ei emailInfo
  49.         for i, name := range p.SubexpNames() {
  50.                 switch name {
  51.                 case "first":
  52.                         // TODO
  53.                 case "last":
  54.                         // TODO
  55.                 case "year":
  56.                         // TODO
  57.                 case "comp":
  58.                         // TODO
  59.                 }
  60.         }
  61.         return ei.String()
  62. }

Quellcode

Hier kannst du den Code kopieren und ihn in deinen bevorzugten Editor einfügen. PASTEBIN_DOWNLOAD_SNIPPET_EXPLAIN