英文:
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:\\\\\\\\"
英文:
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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论