Any way to escape a Go string in a regular expression?

时光总嘲笑我的痴心妄想 提交于 2019-12-08 23:27:30

问题


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.

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


回答1:


regexp.QuoteMeta does the deed.



来源:https://stackoverflow.com/questions/27415532/any-way-to-escape-a-go-string-in-a-regular-expression

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!