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

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

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

问题

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

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

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

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

答案1

得分: 8

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

  1. `C:\\\\`

等同于:

  1. "C:\\\\\\\\"

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

英文:

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

  1. `C:\\\\`

is equivalent to:

  1. "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:

确定