正则表达式在签入github时被转换

问题描述

我在其中一个文件中包含正则表达式,如下所示。

pattern = "^[^,&*\n\t\r]*"

但是当我将其签到github时,它正转换为无效字符,如下图所示。

enter image description here

添加了.gitattributes,如下所示。仍然出现此错误。任何帮助将不胜感激。

* text=auto
*.proto text

解决方法

我建议对每个空白字符使用十六进制表示法:

pattern = "^[^,&*\\x0A\\x0D\\x09]*"

通过这种方式,模式将作为^[^,&*\x0A\x0D\x09]*文本传递,\xNN regex转义将由regex引擎本身而不是语言引擎解析。