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

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

本文介绍了寻找一个正则表达式来它的话分割文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在寻找一个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 作为的),那么你就可以得到在开头和结尾的额外字符串。例如,输入字符串(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.

这篇关于寻找一个正则表达式来它的话分割文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-12 12:36