C Sharp Find a Word
From Regex Regular Expression Encyclopedia
You can use this recipe for finding single words in a block of text. The expression will find only complete words surrounded by spaces or other word delimiters, such as punctuation or the beginning or end of a line.
[edit] code
using System; using System.IO; using System.Text.RegularExpressions; public class Recipe { private static Regex _Regex = new Regex( @"\bsomething\b" ); public void Run(string fileName) { String line; int lineNbr = 0; using (StreamReader sr = new StreamReader(fileName)) { while(null != (line = sr.ReadLine())) { lineNbr++; if (_Regex.IsMatch(line)) { Console.WriteLine("Found match '{0}' at line {1}", line, lineNbr); } } } } public static void Main( string[] args ) { Recipe r = new Recipe(); r.Run(args[0]); } }
[edit] How It Works
A special character class, \b, allows you to easily search for whole words. This is an advantage because without doing a whole bunch of extra work you can make sure that a search for some- thing, for example, doesn’t yield unexpected matches such as somethings. You can break the regular expression shown here into the following:
| Regular Expression | Description |
|---|---|
| \b | a word boundary (a space, beginning of a line, or punctuation) . . . |
| something | s, o, m, e, t, h, i, n, and g . . . |
| \b | a word boundary at the end of the word. |
This expression differs just slightly from the C# and Visual Basic .NET examples because the RegularExpressionValidator control assumes that the expression is to match the entire value (there’s an implied ^ at the beginning of the expression and $ at the end of the expres- sion). The combination .* has been added before and after the word boundary \b so the full word can float around inside the line.
