regex - How do you match character OR end of string in a golang regexp? -
i having trouble working out (in golang), within regular expression, how match character, separator, or end of string. following want:
url := "test20160101" if i, _ := regexp.matchstring("[-a-za-z/]20[012]\\d[01]\\d[0123]\\d[-a-za-z/]", url); == true { t := regexp.mustcompile("[-a-za-z/](20[012]\\d[01]\\d[0123]\\d)[-a-za-z/]").findstringsubmatch(url)[1] fmt.println("match: ", t) }
https://play.golang.org/p/ewz_diovbl
but want match following:
url := "test-20160101-" url := "/20160101/page.html"
i notice there \z in the golang documentation doesn't work, @ least when put inside [-a-za-z/]
i.e. [-a-za-z\\z/]
i interested , played around fun. maybe this: https://play.golang.org/p/grvnhtww0g
package main import ( "fmt" "regexp" ) func main() { // want match "test-20160101", "/20160101/" , "test-20160101" re := regexp.mustcompile(`[-a-za-z/](20[012]\d[01]\d[0123]\d)([-a-za-z/]|\z)`) urls := []string{ "test-20160101", "/20160101/page.html", "test20160101", "nomatch", "test19990101", } _, url := range urls { t := re.findstringsubmatch(url) if len(t) > 2 { fmt.println("match", url, "=", t[1]) } } }
Comments
Post a Comment