doudao8283
2016-02-03 16:56
浏览 262
已采纳

在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?

图片转代码服务由CSDN问答提供 功能建议

我想在Go中的字符串上运行以下正则表达式

<代码> \(([0-9] +),(+),([0-9]:$([0-9] +))\)

,但我不断收到错误未知的转义序列:(

我运行它的字符串是(1, 53.38,$ 45)(2,88.62,$ 98)(3,78.48,$ 3)(4,72.30,$ 76)(5,30.18,$ 9)(6,46.34,$ 48) \ n

所以我的问题是,如何在Go的正则表达式中转义括号?

  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

1条回答 默认 最新

  • douwudie8060 2016-02-03 17:00
    已采纳

    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]+))\)`
    
    打赏 评论

相关推荐 更多相似问题