使用C#从文本中删除数字

我有一个文本文件进行处理,有一些数字。 我想要JUST文本,没有别的。 我设法删除了标点符号,但如何删除这些数字呢? 我想用C#代码。

另外,我想删除长度大于10的单词。如何使用Reg表达式执行此操作?

您可以使用正则表达式执行此操作:

string withNumbers = // string with numbers string withoutNumbers = Regex.Replace(withNumbers, "[0-9]", ""); 

使用此正则表达式删除超过10个字符的单词:

 [\w]{10, 100} 

100定义要匹配的最大长度。 我不知道是否有最小长度的量词…

只有字母而没有别的(因为我看到你也想删除标点符号)

Regex.IsMatch(input, @"^[a-zA-Z]+$");

你也可以使用string.Join:

 string s = "asdasdad34534t3sdf43534"; s = string.Join(null, System.Text.RegularExpressions.Regex.Split(s, "[\\d]")); 

Regex.Replace方法应该可以解决问题。

 // regex to match any digit var regex = new Regex("\d"); // replace all matches in input with empty string var output = regex.Replace(input, String.Empty);