Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Escaping parentheses in Go regexp

Tags:

regex

go

I am tying to run the following regular expression on a string in Go

\(([0-9]+),([0-9.]+),(?:$([0-9]+))\)

but I keep getting the error unknown escape sequence: (

the string that I'm running it on is (1,53.38,$45) (2,88.62,$98) (3,78.48,$3) (4,72.30,$76) (5,30.18,$9) (6,46.34,$48)

So my question is, how do you escape parentheses in Go's regexp?

like image 902
watzon Avatar asked Dec 10 '22 18:12

watzon


1 Answers

You need to escape the backslashes, because \( isn't a valid excape sequence.

"\\(([0-9]+),([0-9.]+),(?:$([0-9]+))\\)"

More commonly you would use backticks for string literals without escaping:

`\(([0-9]+),([0-9.]+),(?:$([0-9]+))\)`
like image 179
JimB Avatar answered Jan 01 '23 14:01

JimB