2015-03-03 96 views
1

執行FindAllStringSubmatch正則表達式匹配,包括不同日期格式的命名組。我無法循環結果。註釋掉的條件使其工作,但不乾淨,並因爲我添加額外的匹配而中斷。我覺得我正在接近它錯誤,並希望一些重定向。 THX/轉,嘗試循環日期正則表達式結果

package main 

import (
    "fmt" 
    "regexp" 
    "strings" 
) 

func main() { 
    text := "12/31/1956 31/11/1960" 
    reg := []string{`(?P<month>1[12])/(?P<day>\d\d)/(?P<year>19\d\d)`, `(?P<day>\d\d)/(?P<month>1[12])/(?P<year>19\d\d)`} 
    // Combine them, case ignored 
    regall := "(?i)" + strings.Join(reg, "|") 
    myExp := regexp.MustCompile(regall) 
    match := myExp.FindAllStringSubmatch(text, -1) 
    fmt.Println("Match", match, len(match)) 
    fmt.Println("Names", myExp.SubexpNames(), len(myExp.SubexpNames())) 
    for i := 0; i < len(match); i++ { 
     result := make(map[string]string) 
     for j, name := range myExp.SubexpNames() { 
      result[name] = match[i][j] 
      //if (result["month"] != "" && result["day"] != "" && result["year"] != "") { 
       fmt.Println(match[i][0],i,j,result["month"] + "/" + result["day"] + "/" + result["year"]) 
      //} 
     } 
    } 
} 

Results in: 
Match [[12/31/1956 12 31 1956 ] [31/11/1960 31 11 1960]] 2 
Names [ month day year day month year] 7 
12/31/1956 0 0 // 
12/31/1956 0 1 12// 
12/31/1956 0 2 12/31/ 
12/31/1956 0 3 12/31/1956 
12/31/1956 0 4 12//1956 
12/31/1956 0 5 //1956 
12/31/1956 0 6 // 
31/11/1960 1 0 // 
31/11/1960 1 1 // 
31/11/1960 1 2 // 
31/11/1960 1 3 // 
31/11/1960 1 4 /31/ 
31/11/1960 1 5 11/31/ 
31/11/1960 1 6 11/31/1960 

回答

0

經過一番反思,並從上面的幫助下,與上述答案的援助,我想出了循環的正則表達式的分開,以便更好地處理重疊的命名捕捉組,我想出了這一點:(張貼他人的利益,歡迎任何批評)

package main 

import (
    "fmt" 
    "os" 
    "regexp" 
    "strconv" 
    "strings" 
) 

func main() { 
    month_d := `(?P<month>1[012]|0?[1-9])` 
    month_t := `(?P<month>jan(?:uary|.)?|feb(?:ruary|.)?|mar(?:ch|.)?|apr(?:il|.)?|may(.)|jun(?:e|.)?|jul(?:y|.)?|aug(?:ust|.)?|sep(?:tember|t|t.|.)?|oct(?:ober|.)?|nov(?:ember|.)?|dec(?:ember|.)?)` 
    day := `(?P<day>3[01]|[12][0-9]|[0]?[1-9])(?:\s)?(?:th|rd|nd|st)?` 
    year := `(?P<year>(?:19|20)?\d\d)` 
    sep_d := `[?:/.-]` 
    sep_t := `[?:,\s]` 

    text := "fedskjnkvdsj February 6 2004 sdffd Jan 12th 56 1/12/2000 2013/12/1 2099/12/5 1/12/1999 dsfjhfdhj" 
    regs := []string{ 
     "(" + month_d + sep_d + day + sep_d + year + ")", 
     "(" + year + sep_d + month_d + sep_d + day + ")", 
     "(" + day + sep_d + month_d + sep_d + year + ")", 
     "(" + month_t + sep_t + day + sep_t + year + ")", 
     "(" + day + sep_t + month_t + sep_t + year + ")", 
    } 
    for i := 0; i < len(regs); i++ { 
     myExp, err := regexp.Compile("(?i)" + regs[i]) 
     if err != nil { 
      fmt.Printf("There is a problem with your regexp.\n") 
      return 
     } 
     match := myExp.FindAllStringSubmatch(text, -1) 
     //fmt.Println("Match", match, len(match)) 
     //fmt.Println("Names", myExp.SubexpNames(), len(myExp.SubexpNames())) 
     for j := 0; j < len(match); j++ { 
      result := make(map[string]string) 
      for k, name := range myExp.SubexpNames() { 
       result[name] = match[j][k] 
      } 
      // Fix Month 
      themonth := strings.ToLower(result["month"]) 

      if len(themonth) == 1 { 
       themonth = "0" + themonth 
      } 
      if len(themonth) >= 3 { 
       themonth = themonth[0:3] 
       switch themonth { 
       case "jan": 
        themonth = "01" 
       case "feb": 
        themonth = "02" 
       case "mar": 
        themonth = "03" 
       case "apr": 
        themonth = "04" 
       case "may": 
        themonth = "05" 
       case "jun": 
        themonth = "06" 
       case "jul": 
        themonth = "07" 
       case "aug": 
        themonth = "08" 
       case "sep": 
        themonth = "09" 
       case "oct": 
        themonth = "10" 
       case "nov": 
        themonth = "11" 
       case "dec": 
        themonth = "12" 
       default: 
        fmt.Println("Month Error: " + themonth) 
        os.Exit(2) 
       } 
      } 
      // Day 
      theday := result["day"] 
      if len(theday) == 1 { 
       theday = "0" + theday 
      } 
      // Fix Year 
      theyear := result["year"] 
      if len(theyear) == 2 { 
       val_year, err := strconv.ParseInt(theyear,10,0) 
       if err != nil { 
        // handle error 
        fmt.Println(err) 
        os.Exit(2) 
       } 
       if val_year > 50 { 
        theyear = "19" + theyear 
       } else { 
        theyear = "20" + theyear 
       } 
      } 
      date := themonth + "/" + theday + "/" + theyear 
      fmt.Println(date) 
     } 
    } 
}