Regular expression to validate image

huangapple go评论127阅读模式
英文:

Regular expression to validate image

问题

我正在使用golang编写一个Web应用程序。我正在使用正则表达式来验证URL。但是我无法在URL验证中验证图像(abc.png)。

var validPath = regexp.MustCompile("^/$|/(home|about|badge)/(|[a-zA-Z0-9]+)$")

上述URL可以接受/home//about/,但无法接受/abc.png。我的意思是.本身不起作用。

我尝试了以下正则表达式,但没有帮助:

var validPath = regexp.MustCompile("^/$|/(home|about|badge|.)/(|[a-zA-Z0-9]+)$")
var validPath = regexp.MustCompile("^/$|/(home|about|badge)(/|.)(|[a-zA-Z0-9]+)$")

我正在尝试匹配http://localhost:8080/badge.png

请问有人可以帮助我吗?

英文:

I am writing a web application in golang. I am using regular expression to validate the URL. But I am not able to validate image (abc.png) in the URL validation.

var validPath = regexp.MustCompile("^/$|/(home|about|badge)/(|[a-zA-Z0-9]+)$")

The above URL takes /home/, /about/ but could not make for /abc.png. I mean . itself not working

I tried the following regex, but it didn't help

var validPath = regexp.MustCompile("^/$|/(home|about|badge|.)/(|[a-zA-Z0-9]+)$")
var validPath = regexp.MustCompile("^/$|/(home|about|badge)(/|.)(|[a-zA-Z0-9]+)$")

And I am trying to match http://localhost:8080/badge.png

Could anyone please help me on this?

答案1

得分: 2

看起来这个正则表达式应该适合你的需求。具体的解释如下:

  • ^/$ - 整个字符串只包含一个 /
  • | - 或者...
  • ^ - 字符串的开头
  • (?:/(home|about|badge))? - 可选的序列,包括 /,后面跟着 homeabout 或者 badge
  • / - 一个 / 符号,后面跟着
  • ((?:badge|abc)\.png|[a-zA-Z0-9]*) - 第一组捕获:
    • (?:badge|abc)\.png - badge 或者 abc,后面跟着 .png
    • | - 或者...
    • [a-zA-Z0-9]* - 零个或多个字母数字字符
  • $ - 字符串的结尾

这是一个 Go 语言的示例代码,你可以在 Go playground demo 中查看。

package main

import "fmt"
import "regexp"

func main() {
    var validPath = regexp.MustCompile(`^/$|^(?:/(home|about|badge))?/((?:badge|abc)\.png|[a-zA-Z0-9]*)$`)

    fmt.Println(validPath.MatchString("/"), validPath.MatchString("/home/"), validPath.MatchString("/about/"), validPath.MatchString("/home/13jia0"), validPath.MatchString("/about/1jnmjan"), validPath.MatchString("/badge.png"), validPath.MatchString("/abc.png"))
    fmt.Println(validPath.MatchString("/nope/"), validPath.MatchString("/invalid.png"), validPath.MatchString("/test/test"))

    m := validPath.FindStringSubmatch("/about/global")
    fmt.Println("validate() :: URL validation path m[1] : ", m[1])
    fmt.Println("validate() :: URL validation path m[2] : ", m[2])
    if m == nil || m[2] != "global" {
        fmt.Println("Not valid")
    }
}

你可以在 regex demo 中查看该正则表达式的效果。

英文:

It appears

^/$|^(?:/(home|about|badge))?/((?:badge|abc)\.png|[a-zA-Z0-9]*)$

should work for you. See the regex demo.

The pattern breakdown:

  • ^/$ - a / as a whole string
  • | - or...
  • ^ - start of string
  • (?:/(home|about|badge))? - optional sequence of / + either home, or about or badge
  • / - a / symbol followed with
  • ((?:badge|abc)\.png|[a-zA-Z0-9]*) - Group 1 capturing:
    • (?:badge|abc)\.png - badge or abc followed with .png
    • | - or...
    • [a-zA-Z0-9]* - zero or more alphanumerics
  • $ - end of string

And here is the Go playground demo.

package main

import "fmt"
import "regexp"

func main() {
	//var validPath = regexp.MustCompile("^/((home|about)(/[a-zA-Z0-9]*)?|[a-zA-Z0-9]+\\.[a-z]+)?$")
	var validPath = regexp.MustCompile(`^/$|^(?:/(home|about|badge))?/((?:badge|abc)\.png|[a-zA-Z0-9]*)$`)

	fmt.Println(validPath.MatchString("/"), validPath.MatchString("/home/"), validPath.MatchString("/about/"), validPath.MatchString("/home/13jia0"), validPath.MatchString("/about/1jnmjan"), validPath.MatchString("/badge.png"), validPath.MatchString("/abc.png"))
	fmt.Println(validPath.MatchString("/nope/"), validPath.MatchString("/invalid.png"), validPath.MatchString("/test/test"))
	
	m := validPath.FindStringSubmatch("/about/global")
	fmt.Println("validate() :: URL validation path m[1] : ", m[1])
	fmt.Println("validate() :: URL validation path m[2] : ", m[2])
	if m == nil || m[2] != "global" {
		fmt.Println("Not valid")
	}
}

答案2

得分: 1

你要找的是以下内容(基于你发布的示例路径):

var validPath = regexp.MustCompile("^/((home|about)(/[a-zA-Z0-9]*)?|[a-zA-Z0-9]+\\.[a-z]+)?$")

带有示例的 Playground

英文:

What you are looking for is the following (based off the example paths you posted):

var validPath = regexp.MustCompile("^/((home|about)(/[a-zA-Z0-9]*)?|[a-zA-Z0-9]+\\.[a-z]+)?$")

Playground with examples

答案3

得分: 0

你可以使用以下正则表达式进行验证:

var validPath = regexp.MustCompile("^\/(home|about|badge)\/[a-zA-Z0-9]+[.][a-z]+$")

提示:我制作了一个灵活的正则表达式,因此它可以接受许多图像格式,如pngjpgjpeg等。

你可以在这里进行测试:正则表达式

英文:

You can validate with the following Regex:

var validPath = regexp.MustCompile("^\/(home|about|badge)\/[a-zA-Z0-9]+[.][a-z]+$")

Ps: I made a flexible Regex, so it accepts a lot of formats of images: png, jpg, jpeg and so on..

You can test it here: Regex

huangapple
  • 本文由 发表于 2016年2月29日 04:25:02
  • 转载请务必保留本文链接:https://go.coder-hub.com/35687826.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定