当前位置:网站首页>Golang uses regularity to match substring functions
Golang uses regularity to match substring functions
2022-06-27 22:01:00 【Zen and the art of computer programming】
const SERVICE_IMPL_RULE = ".*?\\.NewServer\\(new\\((.*?)\\)\\)"
// getServiceImpl return : CompassInsightServiceImpl
//{
{ .ServiceImpl }} = CompassInsightServiceImpl
// svr := insight_service.NewServer(new(CompassInsightServiceImpl))
func getServiceImpl(filePath string) string {
f, err := os.Open(filePath)
if err != nil {
return ""
}
buf := bufio.NewReader(f)
for {
line, err := buf.ReadString('\n')
line = strings.TrimSpace(line)
if strings.Contains(line, ".NewServer(new(") {
byRegex, err := GetOneStringByRegex(line, SERVICE_IMPL_RULE)
if err != nil {
log.Error(err)
return ""
}
return byRegex
}
if err != nil {
if err == io.EOF {
return ""
}
return ""
}
}
}
const SVR_RULE = "svr := (.*?)\\.NewServer\\(new\\(.*?\\)\\)"
// getNewServerCodeLine return : insight_service
// originLine = svr := insight_service.NewServer(new(CompassInsightServiceImpl))
//{
{ .NewServerCodeLine }} = insight_service
//svr := {
{ .NewServerCodeLine }}.NewServer({
{ .ServiceImpl }}, server.WithMiddleware(transform.ServerTransform), server.WithMiddleware(logmv.ServerLog))
//svr := insight_service.NewServer( CompassInsightServiceImpl, server.WithMiddleware(transform.ServerTransform), server.WithMiddleware(logmv.ServerLog))
func getNewServerCodeLine(filePath string) string {
f, err := os.Open(filePath)
if err != nil {
return ""
}
buf := bufio.NewReader(f)
for {
line, err := buf.ReadString('\n')
line = strings.TrimSpace(line)
if strings.Contains(line, ".NewServer(new(") {
byRegex, err := GetOneStringByRegex(line, SVR_RULE)
if err != nil {
log.Error(err)
return ""
}
return byRegex
}
if err != nil {
if err == io.EOF {
return ""
}
return ""
}
}
}
// GetOneStringByRegex Use regular to match substring functions :
// Use cases :
// from img[/static/upload/xxxx/xxx.jpg] In order to get xxxx/xxx.jpg
//url, _ = GetOneStringByRegex(str, "img\\[.*?/static/upload/(.*?)\\]")
func GetOneStringByRegex(str, rule string) (string, error) {
reg, err := regexp.Compile(rule)
if reg == nil || err != nil {
return "", errors.New(" Regular Compile error :" + err.Error())
}
// Extract key information
result := reg.FindStringSubmatch(str)
if len(result) < 1 {
return "", errors.New(" No substring was obtained ")
}
return result[1], nil
}边栏推荐
- [LeetCode]动态规划解分割数组I[Red Fox]
- Système de gestion - itclub (II)
- Summary of Web testing and app testing by bat testing experts
- VMware virtual machine PE startup
- [LeetCode]100. Same tree
- regular expression
- 【MySQL】数据库函数通关教程下篇(窗口函数专题)
- Sharing | intelligent environmental protection - ecological civilization informatization solution (PDF attached)
- 豆沙绿保护你的双眼
- Go from introduction to actual combat - only any task is required to complete (notes)
猜你喜欢
![[leetcode] dynamic programming solution partition array ii[arctic fox]](/img/a1/4644206db3e14c81f9f64e4da046bf.png)
[leetcode] dynamic programming solution partition array ii[arctic fox]

Null pointer exception

真香,自从用了Charles,Fiddler已经被我彻底卸载了

Slow bear market, bit Store provides stable stacking products to help you cross the bull and bear

Go from introduction to practice -- shared memory concurrency mechanism (notes)

Luogu p5706 redistributing fertilizer and house water
![[leetcode] dynamic programming solution split integer i[silver fox]](/img/18/8dc8159037ec1262444db8899cde0c.png)
[leetcode] dynamic programming solution split integer i[silver fox]

STM32CubeIDE1.9.0\STM32CubeMX 6.5 F429IGT6加LAN8720A,配置ETH+LWIP

Interval DP of Changyou dynamic programming

读写分离-Mysql的主从复制
随机推荐
軟件測試自動化測試之——接口測試從入門到精通,每天學習一點點
Slow bear market, bit Store provides stable stacking products to help you cross the bull and bear
The create database of gbase 8A takes a long time to query and is suspected to be stuck
Figure countdownlatch and cyclicbarrier based on AQS queue
语言弱点列表--CWE,一个值得学习的网站
Method of reading file contents by Excel
IO stream code
鲜为人知的mysql导入数据
[LeetCode]动态规划解分割数组I[Red Fox]
xpath
[leetcode] dynamic programming solution partition array ii[arctic fox]
[LeetCode]30. 串联所有单词的子串
不外泄的测试用例设计秘籍--模块测试
GBase 8a OLAP分析函数 cume_dist的使用样例
STM32F107+LAN8720A使用STM32cubeMX配置网络连接+tcp主从机+UDP app
GBase 8a V8版本节点替换期间通过并发数控制资源使用减少对系统影响的方法
regular expression
[leetcode] dynamic programming solution split integer i[silver fox]
单元测试界的高富帅,Pytest框架,手把手教学,以后测试报告就这么做~
分享|智慧环保-生态文明信息化解决方案(附PDF)