在golang中转义字符串中的特殊字符以供正则表达式使用。

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

Escape string special characters for use in golang regexp

问题

我有一个字符数组,像这样:

chars := []string{".", "-", "(", ")"}

当我以常规方式连接它们(strings.Join(chars, "")),然后将其传递给regexp.MustCompile时,会出现错误:

panic: regexp: Compile(`[.-()]`): error parsing regexp: invalid character class range: `.-(`

如何在Golang正则表达式中转义字符串特殊字符,以便将其作为字符而不是正则表达式运算符使用?

英文:

I have an array of characters like this :

chars := []string{".", "-", "(", ")"}

When I join them in regular way (strings.Join(chars, "")) and pass it to regexp.MustCompile, Its panic :

panic: regexp: Compile(`[.-()]`): error parsing regexp: invalid character class range: `.-(`

How can I scape string special characters for use in golang regexp as a character not regexp operator?

答案1

得分: 11

你必须将-项放在数组的开头/结尾,以便-要么出现在字符类的开头[-.()],要么出现在结尾[.()-]

或者在chars数组中转义-"\\-"

注意:这不是唯一的注意事项,^]也必须转义,或者放置在字符类的特定位置。\\始终必须转义。

请参考Go演示

package main

import (
	"fmt"
	"strings"
	"regexp"
)

func main() {
	chars := []string{"]", "^", "\\\\", "[", ".", "(", ")", "-"}
	r := strings.Join(chars, "")
	s := "[Some]-\\(string)^."
	re := regexp.MustCompile("[" + r + "]+")
	s = re.ReplaceAllString(s, "")
	fmt.Println(s)
}

输出:Somestring

请注意,^不能是第一个项,]必须在开头,-必须在结尾。

英文:

You must put the - item at the start/end of the array so that - appears either at the start - [-.()] - or end - [.()-] - of the character class.

Or escape the - in the chars array: "\\-".

NOTE: That is not the only caveat, the ^ and ] must also be escaped, or placed in a specific location in the character class. The \ must be escaped always.

See a Go demo:

package main

import (
	"fmt"
	"strings"
	"regexp"
)

func main() {
	chars := []string{"]", "^", "\\\\", "[", ".", "(", ")", "-"}
	r := strings.Join(chars, "")
	s := "[Some]-\\(string)^."
	re := regexp.MustCompile("[" + r + "]+")
	s = re.ReplaceAllString(s, "")
	fmt.Println(s)
}

Output: Somestring

Note that the ^ must not be the first item, ] must be at the start and - at the end.

答案2

得分: 2

现在有一种更好的方法:

regexp.MustCompile(regexp.QuoteMeta(strings.Join(chars, "")))

regexp.QuoteMeta()会创建一个完全匹配提供的字符串的正则表达式(即它会转义所有正则表达式元字符)。

英文:

Now there is an even better way:

regexp.MustCompile( regexp.QuoteMeta(strings.Join(chars, "")) )

regexp.QuoteMeta() will create a regular expression that matches the string provided exactly (i.e. it will escape all the regex meta-characters).

huangapple
  • 本文由 发表于 2016年12月23日 18:20:41
  • 转载请务必保留本文链接:https://go.coder-hub.com/41299523.html
匿名

发表评论

匿名网友

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

确定