zoukankan      html  css  js  c++  java
  • 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]+
    $`)
    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]+(
    )*$`)
    
        license1 := reg.FindAllString(fileContext, -1)
    
        //==========Rule1:    The input string should use `12. Identification: [a-z0-9]+
    $` 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(`
    `)
        line := reg2.FindAllString(text, -1)
        line2 := reg2.FindAllString(fileContext, -1)
    
        //==========Rule2:    All LICENSE should have same lines(or same count `
    `)==========
        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)
    }
  • 相关阅读:
    托付和事件的使用
    在使用supervisord 管理tomcat时遇到的小问题
    无法安装vmware tools的解决方PLEASE WAIT! VMware Tools is currently being installed on your system. Dependin
    (转)Openlayers 2.X加载高德地图
    (转)openlayers实现在线编辑
    (转) Arcgis for js加载百度地图
    (转)Arcgis for js加载天地图
    (转) 基于Arcgis for Js的web GIS数据在线采集简介
    (转) Arcgis for js之WKT和GEOMETRY的相互转换
    (转)Arcgis for Js之Graphiclayer扩展详解
  • 原文地址:https://www.cnblogs.com/tianxia2s/p/9108237.html
Copyright © 2011-2022 走看看