使用正则表达式和 golang 选择 jpg 图像 [英] Selecting a jpg image with regex and golang
问题描述
我有一个将 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.
这篇关于使用正则表达式和 golang 选择 jpg 图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!