当前位置:网站首页>Golang 使用正则来匹配出子字符串函数
Golang 使用正则来匹配出子字符串函数
2022-06-27 19:18:00 【禅与计算机程序设计艺术】
const SERVICE_IMPL_RULE = ".*?\\.NewServer\\(new\\((.*?)\\)\\)"
// getServiceImpl 返回: 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 返回: 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 使用正则来匹配出子字符串函数:
//使用案例:
//从 img[/static/upload/xxxx/xxx.jpg] 中获取 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("正则Compile错误:" + err.Error())
}
//提取关键信息
result := reg.FindStringSubmatch(str)
if len(result) < 1 {
return "", errors.New("没有获取到子字符串")
}
return result[1], nil
}边栏推荐
- SQL Server for循环用法
- 释放开源数据库创新力量 | 【甘肃】openGauss Meetup圆满结束
- 使用storcli工具配置RAID,收藏这一篇就够了
- VMware vSphere ESXi 7.0安装教程
- 基于微信小程序的高校党员之家服务管理系统系统小程序#毕业设计,党员,积极分子,学习,打卡,论坛
- Very comprehensive dolphin scheduler installation and use documents
- Can Oracle's CTAs bring constraints and other attributes to the new table?
- Flexible IP network test tool -- x-launch
- squid代理服務器
- 100 important knowledge points that SQL must master: combining where clauses
猜你喜欢

JPA踩坑系列之save方法

White whoring red team goby & POC, how do you call white whoring?

抖音的兴趣电商已经碰到流量天花板?

众昂矿业:新能源或成萤石最大应用领域

100 important knowledge points that SQL must master: using functions to process data

强制 20 天内开发 APP 后集体被裁,技术负责人怒批:祝“早日倒闭!”

VMware vSphere ESXi 7.0安装教程

BTC and eth recapture the lost land! Leading the market recovery? Encryption will enter the "ice age"!

Oracle的CTAS能不能将约束等属性带到新表?

“好声音“连唱10年,星空华文如何唱响港交所?
随机推荐
Leetcode 821. Minimum distance of characters (simple) - sequel
1027 Colors in Mars
Can Oracle's CTAs bring constraints and other attributes to the new table?
划重点!国产电脑上安装字体小技巧
Original translation | comparison of machine learning model service tools: kserve, Seldon core and bentoml
Sharing | intelligent environmental protection - ecological civilization informatization solution (PDF attached)
After kotlin wechat payment callback, the interface is stuck and uipagefragmentactivity windowleft is thrown
MySQL速成——第一天--基础入门
JPA踩坑系列之save方法
How to do a good job of gateway high availability protection in the big promotion scenario
动物养殖生产虚拟仿真教学系统|华锐互动
Experiment of love number lesson | phase V - emotion judgment of commodity review based on machine learning method
mysql使用笔记一
Navicat premium connection problem --- host 'XXXXXXXX' is not allowed to connect to this MySQL server
Educational Codeforces Round 108 (Rated for Div. 2)
OpenSSL 编程 一:基本概念
Data platform scheduling upgrade and transformation | operation practice from Azkaban smooth transition to Apache dolphin scheduler
Prospects for enterprise digitalization (38/100)
Is it safe to open an account and buy stocks on the Internet? New to stocks, no guidance
Codeforces Round #723 (Div. 2)