使用正则表达式和 golang 选择 jpg 图像 [英] Selecting a jpg image with regex and golang

查看:45
本文介绍了使用正则表达式和 golang 选择 jpg 图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个将 jpg 文件与正则表达式和 golang 匹配的示例.

I have an example of matching a jpg file with regex and golang.

正则表达式示例

它完美地选择了它,但与我想要的相反,它删除了它而不是保留它.

It selects it perfectly but does the opposite of what I want, it removes it instead of keeping it.

我如何让它成为替代品,因为目前您会看到它删除了 jpg 链接,并将其余部分留在我想要的相反位置,只留下 jpg 链接.

How do I get it to be the substitution as currently you will see it removes the jpg link and leaves the rest where I want the opposite of it only leaving the jpg link.

这用于 xpath 抓取工具中,我希望它仅传递 jpg 链接.

This is used in an xpath scraper where I want it to pass over just the jpg link.

推荐答案

您可以使用 FindStringSubmatch [1]:

You can use FindStringSubmatch [1]:

package main
import "regexp"

const (
s = "background-color:#000000; background-image:url(https://www.sample.com/free-videos/player-images/20170204-01.jpg); background-size: cover; position: relative;"
)

func main() {
   a := regexp.MustCompile(`\(([^)]+)\)`).FindStringSubmatch(s)
   t := a[1]
   println(t == "https://www.sample.com/free-videos/player-images/20170204-01.jpg")
}

然而,在实际代码中,一定要测试len(a).这是一个很常见的任务,因此,如果您有兴趣,也可以使用 xurls [2] 模块.

However, in real code, make sure to test len(a). This is a pretty common task, so if youre interested, a module xurls [2] is available as well.

  1. https://golang.org/pkg/regexp#Regexp.FindStringSubmatch
  2. https://pkg.go.dev/mvdan.cc/xurls/v2

这篇关于使用正则表达式和 golang 选择 jpg 图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆