计算新线存在多少次?

时间:2011-04-22 21:02:22

标签: regex c#-4.0 count

例如,

string="help/nsomething/ncrayons"

输出:

String word count is: 3

这就是我所拥有的,但程序循环遍历该方法多次,看起来我只是创建了最后一个字符串。这是代码块:

Regex regx = new Regex(@"\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*", RegexOptions.IgnoreCase);
MatchCollection matches = regx.Matches(output);
//int counte = 0;
foreach (Match match in matches)
{
    //counte = counte + 1;
    links = links + match.Value + '\n';
    if (links != null)
    {
        string myString = links;
        string[] words = Regex.Split(myString, @"\n");

        word_count.Text = words.Length.ToString();
    }

}

换行符为\n

1 个答案:

答案 0 :(得分:2)

不确定正则表达式是否适用于您的情况,但您可以使用split

string myString = "help/nsomething/ncrayons";
string[] separator = new string[] { "/n" };
string[] result = myString.Split(separator, StringSplitOptions.None);
MessageBox.Show(result.Count().ToString());

使用正则表达式的另一种方式:

string myString = "help/nsomething/ncrayons";
string[] words = Regex.Split(myString, @"/n");
word_count.Text = words.Length;