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 ...
随机推荐
- day 23 网络编程
C/S架构 Client与Server B/S架构 Browser与Server windows上查看端口占用情况 netstat -a OSI七层模型: 其他略...
- widdow.locationg.可跟的参数,获取url不同的地址
- BadgeView 圆形数字提醒 购物车常用
实际上BadgeView这个类就是继承TextView的.很多TextView中设置字体的方法都适用于BadgeView. 1. setTargetView(View) --> 设置哪个控件显示 ...
- 弹框alertView
// 创建一个弹框UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@“标题” message:@“显示的具体内容” delegate:s ...
- [z]c++ 和 java 利用protobuf 通讯
[z]http://andinker.iteye.com/blog/1979428 java端的具体步骤如下: 1.首先下载 下载protobuf 编译工具 http://code.google ...
- CSS背景background图片
一.CSS背景background图片 - TOP 1.背景图片语法background-image:url() 引入背景图片background-repeat:no-repeat 设置背景图 ...
- Jmeter常用脚本开发之Beanshell Sampler
Beanshell Sampler Beanshell介绍:是一种完全符合java语法规范的脚本语言,且又拥有自己的一些语法和方法:是一种松散类型的脚本语言:它执行标准java语句和表达式,另外它还包 ...
- PAT 甲级 1023 Have Fun with Numbers(20)(思路分析)
1023 Have Fun with Numbers(20 分) Notice that the number 123456789 is a 9-digit number consisting exa ...
- PAT 1004 成绩排名 (20)(代码)
1004 成绩排名 (20)(20 分) 读入n名学生的姓名.学号.成绩,分别输出成绩最高和成绩最低学生的姓名和学号. 输入格式:每个测试输入包含1个测试用例,格式为\ 第1行:正整数n 第2行:第1 ...
- PHP swoole process的使用
引入背景:假如我们每天有10000个订单生成,需要同步到仓储系统中去,以前做法是开启一个crontab去跑这些任务,但是发现总有感觉同步效率低,间隔时间都是分钟级别的. 解决方案测试:我们将同步订单的 ...