当前位置:网站首页>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
}边栏推荐
- Common methods of string class
- Yarn中RMApp、RMAppAttempt、RMContainer和RMNode状态机及其状态转移
- 清华大学教授:软件测试已经走入一个误区——“非代码不可”
- Analysis of stone merging
- 软件缺陷管理——测试人员必会
- 有时间看看ognl表达式
- [LeetCode]513. Find the value in the lower left corner of the tree
- 动态刷新mapper看过来
- Express e stack - small items in array
- GBase 8a数据库用户密码安全相关参数汇总
猜你喜欢

管理系统-ITclub(下)
![[MySQL] database function clearance Tutorial Part 2 (window function topic)](/img/03/2b37e63d0d482d5020b7421ac974cb.jpg)
[MySQL] database function clearance Tutorial Part 2 (window function topic)

美团20k软件测试工程师的经验分享

Remote invocation of microservices

∫(0→1) ln(1+x) / (x ² + 1) dx

.NET学习笔记(五)----Lambda、Linq、匿名类(var)、扩展方法

Process control task

登录凭证(cookie+session和Token令牌)

∫(0→1) ln(1+x) / (x² + 1) dx

Summary of Web testing and app testing by bat testing experts
随机推荐
Gbase 8A OLAP analysis function cume_ Example of dist
Magic POI error in reading excel template file
Go from introduction to actual combat - task cancellation (note)
Common problems encountered by burp Suite
使用Jmeter进行性能测试的这套步骤,涨薪2次,升职一次
Gbase 8A OLAP analysis function cume_ Example of dist
Quick excel export according to customized excel Title Template
Go from introduction to practice - error mechanism (note)
不外泄的测试用例设计秘籍--模块测试
Stm32f107+lan8720a use stm32subemx to configure network connection +tcp master-slave +udp app
Go from introduction to practice -- shared memory concurrency mechanism (notes)
regular expression
管理系统-ITclub(上)
Remote invocation of microservices
vmware虚拟机PE启动
matlab查找某一行或者某一列在矩阵中的位置
I think I should start writing my own blog.
IO stream code
QT base64 encryption and decryption
【Redis】零基础十分钟学会Redis