Cyang
Cyang

Reputation: 379

Removing numbers from text using C#

I have a text file for processing, which has some numbers. I want JUST text in it, and nothing else. I managed to remove the punctuation marks, but how do I remove the numbers? I want this using C# code.

Also, I want to remove words with length greater than 10. How do I do that using Reg Expressions?

Upvotes: 10

Views: 18361

Answers (4)

Stefan
Stefan

Reputation: 14880

You can do this with a regex:

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

Use this regex to remove words with more than 10 characters:

[\w]{10, 100}

100 defines the max length to match. I don't know if there is a quantifier for min length...

Upvotes: 31

raveturned
raveturned

Reputation: 2677

The Regex.Replace method should do the trick.

// 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);

Upvotes: 1

Dor Cohen
Dor Cohen

Reputation: 17080

You can also use string.Join:

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

Upvotes: 1

Andrei G
Andrei G

Reputation: 1590

Only letters and nothing else (because I see you also want to remove the punctuation marks)

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

Upvotes: 3

Related Questions