How do you match character OR end of string in a golang regexp?

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

How do you match character OR end of string in a golang regexp?

问题

我在使用golang中的正则表达式时遇到了困难,我想要匹配一个字符、分隔符或者字符串的结尾。下面的代码几乎可以实现我的需求:

url := "test20160101"
if i, _ := regexp.MatchString("[-a-zA-Z/]20[012]\\d[01]\\d[0123]\\d[-a-zA-Z/]", url); i == true {
    t := regexp.MustCompile("[-a-zA-Z/](20[012]\\d[01]\\d[0123]\\d)[-a-zA-Z/]").FindStringSubmatch(url)[1]
    fmt.Println("match: ", t)
}

但是我还想匹配以下内容:

url := "test-20160101-"
url := "/20160101/page.html"

我注意到在golang文档中有一个\z,但是它似乎不起作用,至少当我将它放在[-a-zA-Z/]中时,即[-a-zA-Z\\z/]

英文:

I am having trouble working out (in golang), within a regular expression, how to match a character, separator, or end of string. The following does almost what I want:

url := "test20160101"
if i, _ := regexp.MatchString("[-a-zA-Z/]20[012]\\d[01]\\d[0123]\\d[-a-zA-Z/]", url); i == 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 I want to also match the following:

url := "test-20160101-"
url := "/20160101/page.html"

I notice there is a \z in the golang documentation however that doesn't work, at least when I put it inside [-a-zA-Z/] i.e. [-a-zA-Z\\z/]

答案1

得分: 2

在模式的末尾加上一个?。这意味着前面的项目是可选的。

如果你想将模式锚定在字符串的末尾进行匹配,可以在最后(在?之后)加上$(或\z)。

此外,你应该使用反引号而不是双引号来表示你的正则表达式。这样你就不需要转义反斜杠。

正如@Zan Lynx提到的,只需编译一次正则表达式。

英文:

Put a ? at the end of the pattern. That means the preceding item is optional.

If you want to anchor the pattern to match at the end of the string, put a $ (or \z) at the very end (after the ?).

Also, you should use backquotes instead of double quotes around your RE. That way you don't have to escape the backslashes.

And as @Zan Lynx mentioned, only compile the RE once.

答案2

得分: 2

我对此感兴趣并进行了一些有趣的尝试。可能是这样的:https://play.golang.org/p/GRVnHTwW0g

package main

import (
	"fmt"
	"regexp"
)

func main() {
	
	// 想要匹配 "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",
	}
	
	for _, url := range urls {
		t := re.FindStringSubmatch(url)
		if len(t) > 2 {
			fmt.Println("匹配", url, "=", t[1])
		}
	}
}
英文:

I was interested and played around with it just for fun. Maybe something like this: https://play.golang.org/p/GRVnHTwW0g

package main

import (
	"fmt"
	"regexp"
)

func main() {
	
	// Want to match "test-20160101", "/20160101/" and "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",
	}
	
	for _, url := range urls {
		t := re.FindStringSubmatch(url)
		if len(t) > 2 {
			fmt.Println("match", url, "=", t[1])
		}
	}
}

huangapple
  • 本文由 发表于 2016年8月24日 07:14:50
  • 转载请务必保留本文链接:https://go.coder-hub.com/39112099.html
匿名

发表评论

匿名网友

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

确定