正则表达式排除[除非前面有\

Car*_*ema 9 java regex

如何编写一个正则表达式,接受一个包含任意数字的表达式,除了' [',除非' ['前面有' \'?

例:

this is text \\[ this also [$ this isn't any more    
Run Code Online (Sandbox Code Playgroud)

从上面的文字中," this is text \\[ this also"应该被接受,其余的不应该被接受.我写了类似的东西:

[.[^\\\\[]]*  
Run Code Online (Sandbox Code Playgroud)

排除' ['但不知道如何允许它包含' \\['以及文本的其余部分.

woe*_*ler 5

这将匹配所有不等于[或等于[前面的字符\:

([^\[]|(?<=\\)\[)+
Run Code Online (Sandbox Code Playgroud)

如果您想要对整个字符串进行简单的传递/失败,只需将开始/结束行字符添加到正则表达式:

^([^\[]|(?<=\\)\[)+$
Run Code Online (Sandbox Code Playgroud)