有没有办法逃避正则表达式中的Go字符串?

时间:2022-10-22 11:34:14

I'm wanting to match ^(@|\s)*{{string}}:? whereas {{string}} is dynamically defined. It may have periods and dashes and any number of things in it and I really need for it to be escaped.

我想匹配^(@ | \ s)* {{string}} :?而{{string}}是动态定义的。它可能有周期和破折号以及其中的任何数量,我真的需要它才能被转义。

PHP provides a preg_quote method that escapes all special characters safely. I was wondering if Go provides any sort of analog.

PHP提供了一个preg_quote方法,可以安全地转义所有特殊字符。我想知道Go是否提供任何类型的模拟。

1 个解决方案

#1


22  

regexp.QuoteMeta does the deed.

regexp.QuoteMeta做了这件事。

#1


22  

regexp.QuoteMeta does the deed.

regexp.QuoteMeta做了这件事。