---
title: "C# regular expression to get words between 4 to 10 characters"  
description: "C# regular expression to get words between 4 to 10 characters"  
author: "Takeshi Okada"  
published: 2014-01-30  
updated: 2014-01-30  
canonical: https://www.mindstick.com/forum/1925/c-sharp-regular-expression-to-get-words-between-4-to-10-characters  
category: "c#"  
tags: ["c#"]  
reading_time: 1 minute  

---

# C# regular expression to get words between 4 to 10 characters

I [am trying](https://answers.mindstick.com/qa/36834/which-two-programming-languages-should-i-master-in-if-i-am-trying-to-get-into-google-or-facebook) to get all the words in a string, that are at least 4 [characters](https://answers.mindstick.com/qa/42112/who-is-the-originator-of-avengers-characters) long and less than 10 characters. When I use the following [regular expression](https://www.mindstick.com/articles/11909/java-regex-or-regular-expression-in-java), it just returned the whole string [as one](https://answers.mindstick.com/qa/34642/which-indian-it-company-has-been-recognized-as-one-of-the-world-s-best-employers-by-the-top-employer-institute-for-the-third-consecutive-year) word. Can you please look at the following example and tell me how should I write this regular expression?

[string result](https://www.mindstick.com/forum/12841/in-mvc-how-do-i-return-a-string-result) = "Overfishing, erosion and warmer waters are feeding jellyfish blooms in coastal [regions](https://yourviews.mindstick.com/view/85074/discovering-the-top-5-beautiful-hill-stations-in-india-s-tropical-regions-a-guide-for-planning-your) worldwide. And they're causing damage"

string[] words = Regex.Split(result, @"[\W]{4,10}");

[foreach](https://www.mindstick.com/forum/33870/how-parallel-foreach-works-internally) (string line in words)

{

Console.WriteLine(line);

}

## Replies

### Reply by Pravesh Singh

Hi Takeshi,

Your code isn't working because the pattern will only match a sequence of 4 to 10 consecutive non-word characters, which doesn't appear in the [string](https://www.mindstick.com/articles/1527/string-split-in-c-sharp). So Regex.Split just returns an array containing the original string.

## Try using this pattern:

\b\w{4,10}\b

## For example:

```
string[] words = Regex.Matches(result,@"\b\w{4,10}\b")                     
.Cast<Match>()                     
.Select(m => m.Value)                     
.ToArray();
```

This will match any sequence of 4 to 10 consecutive word characters, surrounded by word boundaries.


---

Original Source: https://www.mindstick.com/forum/1925/c-sharp-regular-expression-to-get-words-between-4-to-10-characters

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
