[英]Regex.Split on specific characters
I'm trying to make my string split on any Non Word Characters (\\W)
including spaces
but not including these characters : .
我正在尝试将我的字符串拆分为任何
Non Word Characters (\\W)
包括spaces
但不包括以下字符: .
, _
, $
. ,
_
, $
。 also I'm trying to keep any delimiters of Regex.Split
except for spaces
还我试图保持任何分隔符
Regex.Split
除了 spaces
how i can do that? 我该怎么做? i've read on many questions but it doesn't make sense.
我读过很多问题,但这没有道理。 here is my latest code :
这是我的最新代码:
string[] result = Regex.Split (source, @"(\W[^(.|_|#|$)])");
EDIT : 编辑:
an example here 这里的一个例子
using System.Text;
i want : 我想要 :
using
_ System.Text
_ ;
using
_ System.Text
_ ;
(current) result is : (当前)结果是:
_
_
_
using
_ System.Text
_ ;
_
using
_ System.Text
_ ;
_ _
You can use the following .NET-specific [\\W-[._$#\\s]])|\\s+
regex and leverage the C# LINQ to remove empty array items: 您可以使用以下特定于.NET的
[\\W-[._$#\\s]])|\\s+
正则表达式,并利用C#LINQ删除空数组项:
var txt = " using System.Text;";
var splts = Regex.Split(txt, @"([\W-[._$#\s]])|\s+").Where(s => s != String.Empty).ToArray();
Output: 输出:
The regex - [\\W-[._$#\\s]])|\\s+
- uses a character class subtraction that is way more efficient than look-arounds as it leverages all optimizations of .NET regex engine. 正则表达式-
[\\W-[._$#\\s]])|\\s+
-使用字符类减法 ,它比环顾四周的效率更高,因为它利用了.NET regex引擎的所有优化功能。 [\\W-[._$#\\s]]
means any non-word character excluding .
[\\W-[._$#\\s]]
表示除之外的任何非单词字符.
, _
, $
, #
, whitespace
. ,
_
, $
, #
, whitespace
。
(?!\.|#|_|\$|\s)(\W)|\s+
试试看, lookahead
将确保它不会被那些字符分开。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.