Regular expression for getting words from string

Regular expression for getting words from string

Here are C#.net and VB.net program to get words from given input string using regular expression. The given string contains numbers and words. Regex pattern [a-zA-Z]+ finds all words except numbers.

Regular-Expression-Words-String

C# program using [a-zA-Z]+ pattern

using System;
using System.Text.RegularExpressions;
namespace Csharp.RandomExample
{
class Program
{
static void Main(string[] args)
{
string input = "This is statement contains 156 and 777 as number";
string regexPattern = @"[a-zA-Z]+";
Regex re = new Regex(regexPattern);
MatchCollection mc = re.Matches(input);
foreach (Match m in mc)
{
Console.WriteLine("Found word is: {0}", m.Value);
}
Console.ReadLine();
}
}
}
Output
Found word is: This
Found word is: is
Found word is: statement
Found word is: contains
Found word is: number

Regular expressions are very simple to use. You just need to know very few syntaxes and hands-on with some query. Build your own pattern and enhance your knowledge.

Tags:

C# DateTime ToString()C# Regular Expression Get Text Between Brackets.net String vs stringAsp.net Convert String To ImageRegular Expression For AlphanumericReplace string in C#Regular expression for date pattern yyyy-mm-dd[C#] Remove Last Character From StringDateTime format Time StringSplit String In C#Linq Convert String Array To Int Array

Author

My name is Satalaj, but people call me Sat. Here is my homepage: . I live in Pune, PN and work as a Software Engineer. I'm former MVP in ASP.net year 2010.
Disclaimer: Views or opinion expressed here are my personal research and it has nothing to do with my employer. You are free to use the code, ideas/hints in your projects. However, you should not copy and paste my original content to other web sites. Feel free to copy or extend the code.
If you want to fight with me, this website is not for you.
 

I'm Satalaj.