首页 > 其他分享 >Go 提取字符串中url,转换为markdown格式并替换

Go 提取字符串中url,转换为markdown格式并替换

时间:2023-10-17 22:46:36浏览次数:27  
标签:www markdown sourceString url urlReMustCompile Go http

 

Go 提取字符串中url,转换为markdown格式并替换

// MakeContentUrlToMarkDown 将字符串中url非markdown格式转[](url)格式
func MakeContentUrlToMarkDown(sourceString string) (resultString string) {
    //urlReMustCompile := regexp.MustCompile(".*(?P<URL>(http|https|ftp):\\/\\/)?((|[\\w-]+\\.)+[a-z0-9]+)(?:(\\/[^/?#]+)*)?(\\?[^#]+)?(#.+)?))")
    urlReMustCompile := regexp.MustCompile(".*[^\\[]+[^\\(]+(?P<URL>((https?://[^\\s]+)([0-9]+|[a-z]+|[A-Z]+|[$+_/=?%]+)+))")
    for urlReMustCompile.MatchString(sourceString) {
        match := urlReMustCompile.FindStringSubmatch(sourceString)
        groupNames := urlReMustCompile.SubexpNames()
        // 转换为map
        for i, name := range groupNames {
            if i != 0 && name != "" { // 第一个分组为空(也就是整个匹配)
                urlString := match[i]
                newUrlMarkdownString := "[点击链接查看](" + urlString + ")"
                sourceString = strings.ReplaceAll(sourceString, urlString, newUrlMarkdownString)
            }
        }
    }
    resultString = sourceString
    return
}
bodyContent9 := "BJ-SC-DC-TOR-186.BJ单slot替换-控制台主机列表http://www.kdocs.cn/l/?a=b&b=ccc&D=cde2bbfJhtyb#testx1国家  访问http://www.kdocs.cn/l/aaax=bc dfasdf"
markdownTestResult := MakeContentUrlToMarkDown(bodyContent9)
fmt.Println("markdownTestResult:", markdownTestResult)
效果:
markdownTestResult: BJ-SC-DC-TOR-186.BJ单slot替换-控制台主机列表[点击链接查看](http://www.kdocs.cn/l/?a=b&b=ccc&D=cde2bbfJhtyb#testx1)国家 访问[点击链接查看](http://www.kdocs.cn/l/aaax=bc) dfasdf

 

标签:www,markdown,sourceString,url,urlReMustCompile,Go,http
From: https://www.cnblogs.com/zhangmingda/p/17770901.html

相关文章

  • 菜鸡go后端开发学习笔记1
        首先了解项目内容及对应的人员:重要的是产品以及前端。1、了解项目,理清逻辑,有什么不通顺的地方不清楚的地方及时的与产品进行沟通。2、在写请求时,主要是前端发送请求给到后端,后端通过逻辑处理获取数据库里面对应的数据,并返回数据。所以请求字段和前端是有交互......
  • Go - Making an HTTP Client Request
    Problem: YouwanttomakeanHTTPrequesttoawebserver.Solution: Usethenet/httppackagetomakeanHTTPrequest. HTTPisarequest-respondprotocol,andservingrequestsisonlyhalfofthestory.Theotherhalfismakingrequests.Thenet/http......
  • Go - Using Templates for Go Web Applications
    Problem: YouwanttouseGo’stemplatingsystemtocreateawebapplication.Solution: Usethehtml/templatepackagetocreateawebapplication. packagemainimport("html/template""net/http")funchello......
  • springboot访问图片本地路径并映射成url
    这几天很头疼,vue不读取图片,src已经动态绑定了,还是访问不到vue本地下图片,于是我就把图片地址改为springboot本地就成功了。 下面是参考博客;springboot访问图片本地路径并映射成url_springboot配置图片访问路径-CSDN博客最后样式 ......
  • Go - Serving Through HTTPS
    Problem: YouwanttoserveyourwebapplicationthroughHTTPS.Solution: Usethehttp.ListenAndServeTLSfunctiontoserveyourwebapplicationthroughHTTPS. HTTPSisnothingmorethanlayeringHTTPontopoftheTransportSecurityLayer(TLS).Thenet......
  • CSDN、掘金、简书博客文章如何转为Markdown?
    1.在CSDN博文页面点击右键,选择“检查”(Google浏览器为例)。 2.在查看器中搜索“article_content”,找到对应内容,点击…复制为outerHTML。  3.打开网址https://tool.lu/markdown/,点击HTML2MD,粘贴html代码,转换成Markdown。 4.大功告成,同理操作掘金、简书或其他平台上博......
  • 从内存使用角度的比较:Go vs Rust
    Go和Rust是最近几年非常火的语言,经常有人问到底该怎么选择,特别是谁更适合搭建网络后台服务,哪一个性能更好,稳定性更高。网络上Go和Rust的比较文章很多,大体上是做一个测试或写几段测试代码,根据运行的时长来比较哪个性能更好,但这种测试可能会陷入误区:1)比来比去,比的是网络IO,因为这种......
  • Go - Creating a JSON Web Service API
    Problem: YouwanttocreateasimplewebserviceAPIthatreturnsJSON.Solution: Usethenet/httppackagetocreateawebserviceAPIandtheencoding/jsonpackagetoencodedatatobesentbackasJSON. You’llcreateawebserviceAPIthatreturnsa......
  • RunnerGo UI自动化使用体验
    首先需要进入官网,RunnerGo支持开源,可以自行下载安装,也可以点击右上角体验企业版按钮快速体验点击体验企业版进入工作台后可以点击页面上方的UI自动化进入到测试页面创建元素我们可以在元素管理中创建我们测试时需要的元素这里我们以一个打开百度搜索的场景,添加了百度输入框和百度......
  • Go - Serving Static Files
    Problem: Youwanttoservestaticfilessuchasimages,CSS,andJavaScriptfiles.Solution: Usethehttp.FileServerfunctiontoservestaticfiles. funcmain(){dir:=http.Dir("./static")fs:=http.FileS......