ASP.NET Find a Word

From Regex Regular Expression Encyclopedia

Jump to: navigation, search

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

<%@ Page Language="vb" AutoEventWireup="false" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
<head><title></title>
</head>
<body>
    <form Id="Form1" RunAt="server">
    <asp:TextBox id="txtInput" runat="server"></asp:TextBox>
    <asp:RegularExpressionValidator Id="revInput" RunAt="server"
        ControlToValidate="txtInput"
        ErrorMessage="Please enter a valid value"
        ValidationExpression=".*\bsomething\b.*"></asp:RegularExpressionValidator>
    <asp:Button Id="btnSubmit" RunAt="server" CausesValidation="True"
        Text="Submit"></asp:Button>
    </form>
</body>
</html>

[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.

Personal tools