go语言判断末尾不同的长字符串的方法
判断两种末尾不同的长字符串,在使用正则表达式的基础上,进一步利用好字符串的方法,最后成功对问题进行解决。
package utils import (
"io/ioutil"
"os"
"regexp"
"strings"
) //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) reg := regexp.MustCompile(`. Identification: [a-z0-]+\n$`)
//Get the stand LICENSE string
license := reg.FindAllString(text, -)
license1 := reg.FindAllString(fileContext, -) if license1 == nil {
return false, nil
} str := strings.Replace(text, license[], ``, -)
str1 := strings.Replace(fileContext, license1[], ``, -) if str != str1 {
return false, nil
}
return true, nil
}
进一步的添加判断条件
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(`. Identification: [a-z0-]+(\n)*$`) license1 := reg.FindAllString(fileContext, -) //==========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, -)
line2 := reg2.FindAllString(fileContext, -) //==========Rule2: All LICENSE should have same lines(or same count `\n`)==========
if line[] != line2[] {
return false, nil
} reg3 := regexp.MustCompile(`[.&;]`)
point := reg3.FindAllString(text, -)
point2 := reg3.FindAllString(fileContext, -) //==========Rule3: All LICENSE should have same points(or same count `.&;`)==========
if point[] != point2[] {
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, -)
info2 := reg4.FindAllString(Tempb, -) //==========Rule4: The some blocks information should be same(LicenseSummary.*mustbemade)==========
if info[] != info2[] {
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)
}
go语言判断末尾不同的长字符串的方法的更多相关文章
- PHP判断变量是否为长整形的方法
PHP判断变量是否为长整形的方法,可用于判断QQ号等,避免了int溢出的问题 <?php /** * 判断变量是否为长整数(int与整数float) * @param mixed $var * ...
- C 长字符串换行方法
C中字符串有时候会出现很长的情况,如果不换行书写查看起来很不方便. 长字符串拆分成多行处理也是C规范的一部分. 方法1. 利用双引号" " ,将长字符串分成多个子串换行,C会自动无 ...
- c语言判断打开文件是否为空的方法
void writeReslut2(char* caseName,double averageTime,double max, double min,int loops,int size){ fpos ...
- TSQL:判断某较短字符串在较长字符串中出现的次数。
给定一个较短字符串shortStr='ab',和一个较长字符串longStr='adkdabkwelabwkereabrsdweo2342ablk234lksdfsdf1abe': 判断shortSt ...
- YTU 2420: C语言习题 不等长字符串排序
2420: C语言习题 不等长字符串排序 时间限制: 1 Sec 内存限制: 128 MB 提交: 460 解决: 239 题目描述 在主函数中输入n(n<=10)个不等长的字符串.用另一函 ...
- YTU 2419: C语言习题 等长字符串排序
2419: C语言习题 等长字符串排序 时间限制: 1 Sec 内存限制: 128 MB 提交: 650 解决: 249 题目描述 在主函数中输入n(n<=10)个等长的字符串.用另一函数对 ...
- C语言判断字符串是否是 hex string的代码
把写内容过程中经常用到的一些内容段备份一下,如下内容内容是关于C语言判断字符串是否是 hex string的内容. { static unsigned int hex2bin[256]={0}; me ...
- C语言 · 最长字符串
算法训练 最长字符串 时间限制:1.0s 内存限制:512.0MB 求出5个字符串中最长的字符串.每个字符串长度在100以内,且全为小写字母. 样例输入 one two three ...
- Java & PHP & Javascript 通用 RSA 加密 解密 (长字符串)
系统与系统的数据交互中,有些敏感数据是不能直接明文传输的,所以在发送数据之前要进行加密,在接收到数据时进行解密处理:然而由于系统与系统之间的开发语言不同. 本次需求是生成二维码是通过java生成,由p ...
随机推荐
- eclipse配置mybatis xml文件自动提示(转)
原文链接 原文如下: 如果使用eclipse中,再写mybatis的xml文件的时候,没有提示,用“Alt+/”,不能把代码用快捷键敲出来,可以试试下面要说的这种方法,反正我试了,我这个可以. 1.下 ...
- LAB3 整数相加
//yuec2 Yue Cheng package lab3; public class Fraction { int numerator; int denominator; //obeject wi ...
- VS2008水晶报表变两页(重装系统后)
找到水晶报表中的设置-打印机设置,发现打印机名称是 Microsoft XPS Document Writer (已重定向2),
- 细说SVN集中式版本控制器
svn全称Subversion,实现多人开发同一个项目时,对源代码进行管理的工具.在公司里边,一个项目是由多人同时在开发,大家在本地自己电脑开发php代码,完毕后就commit上传给服务器运行. 如 ...
- sap 给集团分配一个逻辑系统
1.进入事务代码 SALE定义一个新的逻辑系统 2.通过事务代码RZ10 进入之后, 将参数login/no_automatic_user_sapstar 修改为“0”, 然后重启SAP服务生效 3. ...
- blockchain good article
https://medium.com/programmers-blockchain/create-simple-blockchain-java-tutorial-from-scratch-6eeed3 ...
- Java中的forin语句
forin的原理 forin语句是JDK5版本的新特性,在此之前,遍历数组或集合的方法有两种:通过下标遍历和通过迭代器遍历.先举个例子: @Test public void demo() { Stri ...
- 同一个线程多次调用start()会出现的问题
结果: 线程首先会运行一次,然后抛出java.lang.IllegalThreadStateException异常. 根据控制台的异常信息,定位到Thread.java的第708行,也就start() ...
- mtcp的快速编译(连接)
mtcp的快速编译 http://mos.kaist.edu/guide/config/03_build_mtcp.html 介绍DPDK中使用mtcp的文档 https://dpdksummit.c ...
- iOS.AutomatePackageBuild.0-Resource-List
1. xcodebuild xcodebuild: 构建Xcode project和workspace. 2. TN2339: Building from the Command Lline with ...