当前位置:网站首页>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
}边栏推荐
- Codeforces Round #719 (Div. 3)
- Love number experiment | Issue 7 - Financial Crisis Analysis Based on random forest
- 体验Navicat Premium 16,无限重置试用14天方法(附源码)
- 覆盖接入2w+交通监测设备,EMQ 为深圳市打造交通全要素数字化新引擎
- Cerebral cortex: predicting children's mathematical skills from task state and resting state brain function connections
- KDD 2022 | 图“预训练、提示、微调”范式下的图神经网络泛化框架
- Yu Wenwen, Hu Xia and other stars take you to play with the party. Pipi app ignites your summer
- Love math experiment | phase VI - Financial anti fraud case study
- MySQL usage notes 1
- “好声音“连唱10年,星空华文如何唱响港交所?
猜你喜欢

100 important knowledge points that SQL must master: sorting and retrieving data

Covering access to 2w+ traffic monitoring equipment, EMQ creates a new digital engine for all elements of traffic in Shenzhen

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

SQL Server for循环用法

Modify large online games through CE modifier

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

BTC和ETH重新夺回失地!引领市场复苏?加密将步入“冰河时代”!

Unity3d button adapts the size according to the text content

Unity3D Button根据文本内容自适应大小

Show the comprehensive strength of strong products, and make the first show of 2022 Lincoln aviator in Southwest China
随机推荐
MySQL client tools are recommended. I can't imagine that it is best to use Juran
实际工作中用到的shell命令 - sed
Can Oracle's CTAs bring constraints and other attributes to the new table?
Shuttle hides the return button of the AppBar
华为伙伴暨开发者大会2022开源时刻全纪录
Unity3D Button根据文本内容自适应大小
GFS分布式文件系统
100 important knowledge points for SQL: in operator
安装gatewayworker之后启动start.php
分享|智慧环保-生态文明信息化解决方案(附PDF)
Codeforces Global Round 14
农产品期货怎么做怎么开户,期货开户手续费多少,找谁能优惠手续费?
开启生态新姿势 | 使用 WrodPress 远程附件存储到 COS
TypeScript学习
GoLand permanently activated
After kotlin wechat payment callback, the interface is stuck and uipagefragmentactivity windowleft is thrown
灵活的IP网络测试工具——— X-Launch
GFS distributed file system
今晚战码先锋润和赛道第2期直播丨如何参与OpenHarmony代码贡献
行业案例|从零售之王看银行数字化转型的运营之道