我正在使用常规表达式来帮助在 Visual Studio 2012 中查找/替换。
根据 msdn,(?([^\r\n])\s)匹配除换行符以外的任何空白字符。
但我不明白它是如何详细工作的。
我只知道[^\r\n]排除换行符,\s匹配任何空格。
外界(?)让我困惑,在 msdn 上找不到任何关于它的信息。
有人可以解释一下吗?或者给我一个链接来咨询。
你的正则表达式是错误的。仅当前\s面有正面或负面的前瞻时才有效。
(?:(?=[^\r\n])\s)
Run Code Online (Sandbox Code Playgroud)
上面的正则表达式的意思是,匹配一个空格字符,但它不会是\n或\r
解释:
(?: group, but do not capture:
(?= look ahead to see if there is:
[^\r\n] any character except: '\r' (carriage
return), '\n' (newline)
) end of look-ahead
\s whitespace (\n, \r, \t, \f, and " ")
) end of grouping
Run Code Online (Sandbox Code Playgroud)
或者
(?:(?![\r\n])\s)
Run Code Online (Sandbox Code Playgroud)
您也可以通过负前瞻实现相同的效果。
解释:
(?: group, but do not capture:
(?! look ahead to see if there is not:
[\r\n] any character of: '\r' (carriage
return), '\n' (newline)
) end of look-ahead
\s whitespace (\n, \r, \t, \f, and " ")
) end of grouping
Run Code Online (Sandbox Code Playgroud)