寻找一个正则表达式来它的话分割文本的话、文本、正则表达式

由网友(你很特别)分享简介:我正在寻找一个RegularEx pression到它的话分割文本。我已经测试I am searching for a RegularExpression to split a text in it words.I have testedRegex.Split(text, @"\s+")但是,这给了我为例子B...

我正在寻找一个RegularEx pression到它的话分割文本。 我已经测试

I am searching for a RegularExpression to split a text in it words. I have tested

Regex.Split(text, @"s+")

但是,这给了我为例子

But this gives me for example for

this (is a) text. and

this
(is
a)
text
and

不过,我寻找一个解决方案,这给了我只有四个字 - 无(,)。等等 它也应该像分割文本

But I search for a solution, that gives me only the words - without the (, ), . etc. It should also split a text like

end.begin

在两个词。

推荐答案

你可能会更好过的匹配的话的,而不是分裂。

You're probably better off matching the words rather than splitting.

如果您使用分割(用 W 作为的 Regexident建议),那么你就可以得到在开头和结尾的额外字符串。例如,输入字符串(AB)会给你的四的输出:B,而另一,因为你使用为分隔。的

If you use Split (with W as Regexident suggested), then you could get an extra string at the beginning and end. For example, the input string (a b) would give you four outputs: "", "a", "b", and another "", because you're using the ( and ) as separators.

你可能想要做的就是匹配的话。你可以做到这一点是这样的:

What you probably want to do is just match the words. You can do that like this:

Regex.Matches(text, "w+").Cast<Match>().Select(match => match.Value)

然后你会得到公正的话,在开始和结束时没有多余的空字符串。

Then you'll get just the words, and no extra empty strings at the beginning and end.

阅读全文

相关推荐

最新文章