问题描述:

This question already has an answer here:

  • How to get only letters from a string in C#?

    4 answers

网友答案:

try this one:

var matches = Regex.Matches(textBox1.Text, "\\w+").Cast<Match>().Select(match => match.Value);

should give you all words without empty strings.

whole code

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;

namespace ConsoleApplication1
{
  class Program
  {
    static void Main(string[] args)
    {

      var matches = Regex.Matches("Line 1 this is any random text. \r\n Line 2 Another Line?! \r\n Line 3 End of text. ", "\\w+").Cast<Match>().Select(match => match.Value);
      foreach (string sWord in matches)
      {
        Console.WriteLine(sWord);
      }

    }
  }
}
网友答案:

If you're wanting to do it with a Regex, and specifically want only letters, you could do this (matching rather than splitting):

var words = Regex.Matches(Test, @"[a-zA-Z]+");"

You may want "[\w]+" instead, as I suspect there will by some characters/digits that you're after.

相关阅读:
Top