我有以下 C# 代码:
var sentence = "As a result , he failed the test .";
var pattern = new Regex();
var outcome = pattern.Replace(sentence, String.Empty);
我应该对正则表达式做什么才能获得以下输出:
结果他没通过考试。
如果您想将英文中空格后通常不会出现的标点符号列入白名单,您可以使用:
\s+(?=[.,?!])
\s+
- 所有空白字符。您可能需要 [ ]+
。(?=[.,?!])
- 前瞻。下一个字符应该是 .
、,
、?
或 !
.您需要在代码中添加一个模式来匹配标点符号之前的空格:
var sentence = "As a result , he failed the test .";
var pattern = new Regex(@"\s+(\p{P})");
var outcome = pattern.Replace(sentence, "$1");
输出: