当前位置:网站首页>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
}边栏推荐
- Here are 12 commonly used function formulas for you. All used ones are good
- 银河麒麟系统局域网文件共享教程
- SQL必需掌握的100个重要知识点:使用函数处理数据
- Share an experience of self positioning + problem solving
- BTC和ETH重新夺回失地!引领市场复苏?加密将步入“冰河时代”!
- SQL必需掌握的100个重要知识点:过滤数据
- Shell command used in actual work - sed
- 基于微信小程序的高校毕业论文管理系统#毕业设计
- 100 important knowledge points that SQL must master: retrieving data
- mime. Type file content
猜你喜欢

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

动物养殖生产虚拟仿真教学系统|华锐互动

Squid proxy server

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

Modify large online games through CE modifier

GoLand permanently activated

SQL Server for循环用法

MySQL performance optimization index function, hidden, prefix, hash index usage (2)

麒麟V10安装字体

GFS distributed file system
随机推荐
MySQL usage notes 1
Very comprehensive dolphin scheduler installation and use documents
TypeScript学习
100 important knowledge points that SQL must master: filtering data
灵活的IP网络测试工具——— X-Launch
Love math experiment | Issue 8 - building of Singapore house price prediction model
于文文、胡夏等明星带你玩转派对 皮皮APP点燃你的夏日
Yu Wenwen, Hu Xia and other stars take you to play with the party. Pipi app ignites your summer
Save method of JPA stepping pit series
众昂矿业:新能源或成萤石最大应用领域
Open a new ecological posture | use the wrodpress remote attachment to store it in COS
MySQL Express - day 1 - basic introduction
MySQL客户端工具推荐,一定想不到最好用巨然是它
GFS distributed file system
Galaxy Kirin system LAN file sharing tutorial
爱数课实验 | 第八期-新加坡房价预测模型构建
100 important knowledge points that SQL must master: creating calculation fields
OpenSSL 编程 二:搭建 CA
Animal breeding production virtual simulation teaching system | Sinovel interactive
Goldfish rhca memoirs: do447 managing projects and carrying out operations -- creating job templates and starting jobs