人生列车

follow on!success!

导航

go语言判断末尾不同的长字符串的方法

判断两种末尾不同的长字符串,在使用正则表达式的基础上,进一步利用好字符串的方法,最后成功对问题进行解决。

 1 package utils
 2 
 3 import (
 4     "io/ioutil"
 5     "os"
 6     "regexp"
 7     "strings"
 8 )
 9 
10 //IsLICENSE return true when file is right LICENSE format while return false when the file is wrong format
11 func IsLICENSE(filepath string, fileContext string) (bool, error) {
12     _, err := os.Open(filepath)
13     if err != nil {
14         return false, err
15     }
16     buff, err := ioutil.ReadFile(filepath)
17     text := string(buff)
18 
19     reg := regexp.MustCompile(`12. Identification: [a-z0-9]+\n$`)
20     //Get the stand LICENSE string
21     license := reg.FindAllString(text, -1)
22     license1 := reg.FindAllString(fileContext, -1)
23 
24     if license1 == nil {
25         return false, nil
26     }
27 
28     str := strings.Replace(text, license[0], ``, -1)
29     str1 := strings.Replace(fileContext, license1[0], ``, -1)
30 
31     if str != str1 {
32         return false, nil
33     }
34     return true, nil
35 }

 进一步的添加判断条件

package utils

import (
    "io/ioutil"
    "os"
    "regexp"
    "strings"

    "bytes"
    "unicode"
)

//IsLICENSE return true when file is right LICENSE format while return false when the file is wrong format
func IsLICENSE(filepath string, fileContext string) (bool, error) {
    _, err := os.Open(filepath)
    if err != nil {
        return false, err
    }
    buff, err := ioutil.ReadFile(filepath)
    text := string(buff)

    //use this regexp to find the end
    reg := regexp.MustCompile(`12. Identification: [a-z0-9]+(\n)*$`)

    license1 := reg.FindAllString(fileContext, -1)

    //==========Rule1:    The input string should use `12. Identification: [a-z0-9]+\n$` as end.==========
    //The len of the license1 must be 0 or 1.        0 means not found this end.
    if license1 == nil {
        return false, nil
    }

    //cut the end and the begin should be equal(This is a wrong rule)
    // str := strings.Replace(text, license[0], ``, -1)
    // str1 := strings.Replace(fileContext, license1[0], ``, -1)
    // if str != str1 {
    //     return false, nil
    // }

    reg2 := regexp.MustCompile(`\n`)
    line := reg2.FindAllString(text, -1)
    line2 := reg2.FindAllString(fileContext, -1)

    //==========Rule2:    All LICENSE should have same lines(or same count `\n`)==========
    if line[0] != line2[0] {
        return false, nil
    }

    reg3 := regexp.MustCompile(`[.&;]`)
    point := reg3.FindAllString(text, -1)
    point2 := reg3.FindAllString(fileContext, -1)

    //==========Rule3:    All LICENSE should have same points(or same count `.&;`)==========
    if point[0] != point2[0] {
        return false, nil
    }

    //delete all char can not see in text and fileContext
    Tempa := DeleteNoSeeCharInString(text)
    Tempb := DeleteNoSeeCharInString(fileContext)

    reg4 := regexp.MustCompile(`LicenseSummary.*mustbemade`)
    info := reg4.FindAllString(Tempa, -1)
    info2 := reg4.FindAllString(Tempb, -1)

    //==========Rule4:    The some blocks information should be same(LicenseSummary.*mustbemade)==========
    if info[0] != info2[0] {
        return false, nil
    }

    return true, nil
}

//ArrayStr2String turn string array to string append
func ArrayStr2String (input []string) string {
    var buffer bytes.Buffer
    for _,v := range input{
        buffer.WriteString(v)
    }
    return buffer.String()
}

//DeleteNoSeeCharInString delete all char can not see in text and fileContext
func DeleteNoSeeCharInString(input string) string {
    temp := strings.FieldsFunc(input, unicode.IsSpace)    
    return ArrayStr2String(temp)
}

 

posted on 2018-05-29 22:13  tianxia2s  阅读(174)  评论(0编辑  收藏  举报