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 ...
随机推荐
- 【校招面试 之 C/C++】第14题 C++ 内存分配方式详解——堆、栈、自由存储区、全局/静态存储区和常量存储区(堆栈的区别)
栈,就是那些由编译器在需要的时候分配,在不需要的时候自动清除的变量的存储区.里面的变量通常是局部变量.函数参数等.在一个进程中,位于用户虚拟地址空间顶部的是用户栈,编译器用它来实现函数的调用.和堆一样 ...
- width多少,超过了用....表示
maxWidth:'140px',whiteSpace:'nowrap',overflow:'hidden',textOverflow:'ellipsis'
- 34-BigInteger详解
在用C或者C++处理大数时感觉非常麻烦,但是在Java中有两个类BigInteger和BigDecimal分别表示大整数类和大浮点数类,至于两个类的对象能表示最大范围不清楚,理论上能够表示无线大的数, ...
- 1-跑Faster R-CNN项目中的一些问题
原理介绍: https://blog.csdn.net/quincuntial/article/details/79132243 我用的环境: Python 3.5.2 cpu版的ten ...
- node.js中对Event Loop事件循环的理解
javascript是单线程的,所以任务的执行都需要排队,任务分为两种,一种是同步任务,一种是异步任务. 同步任务是进入主线程上排队执行的任务,上一个任务执行完了,下一个任务才会执行. 异步任务是不进 ...
- PAT 1042 字符统计(20)(思路)
1042 字符统计(20)(20 分) 请编写程序,找出一段给定文字中出现最频繁的那个英文字母. 输入格式: 输入在一行中给出一个长度不超过1000的字符串.字符串由ASCII码表中任意可见字符及空格 ...
- python多线程下载网页图片并保存至特定目录
#!python3 #multidownloadXkcd.py - Download XKCD comics using multiple threads. import requests impor ...
- Luogu 2173 [ZJOI2012]网络 - LCT
Solution $LCT$ 直接上$QuQ$ 注意$cut$ 完 需要 $d[u + c * N]--$ 再 $link$, 不然会输出Error 1的哦 Code #include<cs ...
- python正则表达式获取两段标记内的字符串
比如获取绿色字符串 ModelData.PayTableData =[{"}, {"}, {"}]; ModelData.PayTableData1 =[{"} ...
- openssl pem密钥文件rsa加密解密例子
准备工作 命令行加密解密,用与比对代码中的算法和命令行的算法是否一致 C:\openssl_test>openssl rsautl -encrypt -in data.txt -inkey pu ...