所以我想使用基本上是 \s
(空格)的模式,只是我不想匹配不间断空格 (\160
)。这将是一个非常长的字符串,用于定义除该字符之外的所有空白字符。有没有办法定义自定义模式并为其提供委托(delegate)?它只是 c => Char.IsWhiteSpace(c) && c != '\160'
谢谢。
最佳答案
在 .NET 正则表达式中,您可以使用 character class subtraction .
It makes it easy to match any single character present in one list (the character class), but not present in another list (the subtracted class). The syntax for this is
[class-[subtract]]
. If the character after a hyphen is an opening bracket, these flavors interpret the hyphen as the subtraction operator rather than the range operator. You can use the full character class syntax within the subtracted character class.
要从 \s
类中仅排除硬空间 (\u00A0
),请使用
[\s-[\u00A0]]
作为替代方案(因为字符类减法是 .NET 特定的功能),您始终可以依靠负向前瞻来限制泛型类(尽管此处的性能较差):
(?!\u00A0)\s
关于c# - 我可以为 C# 正则表达式定义自定义模式吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33041071/