What is the difference between a regular expression quoted by ` and "?

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

What is the difference between a regular expression quoted by ` and "?

问题

为什么 C:\\(由 ` 引用)[tag:regexp] 不能匹配 "C:\\""C:\\\\"

r, err := regexp.Compile(`C:\\\\`) // 不匹配
r, err := regexp.Compile("C:\\\\")  // 匹配
if r.MatchString("Working on drive C:\\") == true {
    fmt.Printf("匹配。") 
} else {
    fmt.Printf("不匹配。")
}
英文:

Why the C:\\\\ (quoted by `) [tag:regexp] does not match "C:\\" and "C:\\\\" do ?

r, err := regexp.Compile(`C:\\\\`) // Not match
r, err := regexp.Compile("C:\\\\")  // Matches
if r.MatchString("Working on drive C:\\") == true {
    fmt.Printf("Matches.") 
} else {
    fmt.Printf("No match.")
}

答案1

得分: 8

原始字符串字面值中的转义序列(由引号引起来)不会被解释。

`C:\\\\`

等同于:

"C:\\\\\\\\"

请参阅《Go编程语言规范-字符串字面值》

英文:

Escape sequences in raw string literal (quoted by quotes) are not interpreted.

`C:\\\\`

is equivalent to:

"C:\\\\\\\\"

See The Go Programming Language Specification - String literals.

huangapple
  • 本文由 发表于 2013年12月24日 20:32:22
  • 转载请务必保留本文链接:https://go.coder-hub.com/20761016.html
匿名

发表评论

匿名网友

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

确定