gpt4 book ai didi

c# - 我可以为 C# Regex 定义自定义模式吗?

转载 作者:太空宇宙 更新时间:2023-11-03 10:22:11 25 4
gpt4 key购买 nike

所以我想使用基本上是\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]]

参见 regex demo

作为替代方案(因为字符类减法是 .NET 特定的功能),您始终可以依靠否定的前瞻来限制泛型类(尽管这里的性能更差):

(?!\u00A0)\s

参见 another demo

关于c# - 我可以为 C# Regex 定义自定义模式吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33041071/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com