我在日常工作中处理了很多正则表达式。听起来很奇怪,有时我什至使用 RegEx 来编辑/修复/格式化我的 RegEx 表达式。然而,这是困扰我的一个问题。我如何正确捕获转义字符,并且只有那些真正转义的字符?
字符串列表:
this is a test
this is a te\st
this is a te\\st
this is a te\\\st
this is a te\\\\st
this is a te\\\\\st
this is a te\\\\\\st
如果我只想匹配“s”是(或不是)字符类(即空格)的那些,我该怎么做?
解释:
this is a test = test
this is a te\st = te \s t
this is a te\\st = te \\ st
this is a te\\\st = te \\ \s t
this is a te\\\\st = te \\ \\ st
this is a te\\\\\st = te \\ \\ \s t
this is a te\\\\\\st = te \\ \\ \\ st
您不能简单地使用 a[^\\]s
或(?<!\\)s
。我尝试了多种组合,但没有成功。我如何捕捉:
this is a test
this is a te\\st
this is a te\\\\st
this is a te\\\\\\st
和/或相反:
this is a te\st
this is a te\\\st
this is a te\\\\\st
我尝试过的变体。. .
.*(?<=(?<!\\)(?<=(\\\\)+))st.*
.*((?<=(?<!\\)(\\\\)+)|(?<!\\))st.*
编辑:这需要是动态长度。