Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

regex

go

Why the C:\\\\ (quoted by `) 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.")
}
like image 354
Salah Eddine Taouririt Avatar asked Dec 24 '13 12:12

Salah Eddine Taouririt


1 Answers

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.

like image 128
falsetru Avatar answered Nov 05 '22 12:11

falsetru