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 ...
随机推荐
- Delphi: Class Static Methods
在Delphi中,自Delphi 2007之后,支持static形式的class方法,样式比如: type TMyClass = class strict private class var FX: ...
- 阿里云视频直播API签名机制源码
阿里云视频直播API签名机制源码 本文展示:通过代码实现下阿里视频直播签名处理规则 阿里云视频直播签名机制,官方文档链接:https://help.aliyun.com/document_detail ...
- [leetcode]128. Longest Consecutive Sequence最长连续序列
Given an unsorted array of integers, find the length of the longest consecutive elements sequence. Y ...
- linux命令学习之:cp
cp命令用来复制文件或者目录,是Linux系统中最常用的命令之一. 如同时指定两个以上的文件或目录,且最后的目的地是一个已经存在的目录,则它会把前面指定的所有文件或目录复制到此目录中.若同时指定多个文 ...
- Django日志系统
在Django中使用的日志系统是基于Python中的loggin模块. 首先简单介绍下logging. 一 Loggin模块简介 loggin模块主要包含以下四个部分: Loggers ...
- Expression #1 of ORDER BY clause is not in GROUP BY clause and contains nonaggregated column 'information_schema.PROFILING.SEQ' which is not functionally dependent on columns in GROUP BY clause; this
最近使用新版本的mysql,执行语句的时候报错.网上找了一下,解决方法如下: vim /etc/mysql/conf.d/mysql.cnf [mysqld] sql_mode=STRICT_TRAN ...
- Django——模板语言相关内容
Django模板语言相关内容 Django模板系统 官方文档 常用语法 只需要记两种特殊符号: {{ }}和 {% %} 变量相关的用{{}},逻辑相关的用{%%}. 变量 {{ 变量名 }} ...
- android使用Pull解析来自服务器的xml文件时出现错误以及解决方案
直接上代码,代码中有详细注释: 1 public class CheckUpdateManager { 2 private static final String TAG = "CheckU ...
- phpstorm+xdebug调试代码
1工具 #phpstorm 前面有文章介绍如何安装 #phpStudy 官网下的2018最新的安装包,php环境使用的也是最新的php7.0nts 2开启php Xdebug拓展 开启拓展,phpSt ...
- ubuntu增加工作分区命令
参考自: https://www.cnblogs.com/wolflowhereu/p/5422653.html ubuntu增加工作分区(workspace)命令 dconf write /org ...