ASP.NET Search for Lines Beginning with a Word
From Regex Regular Expression Encyclopedia
This recipe allows you to find whole words at the beginning 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="Moo\b.*"></asp:RegularExpressionValidator> <asp:Button Id="btnSubmit" RunAt="server" CausesValidation="True" Text="Submit"></asp:Button> </form> </body> </html>
[edit] How It Works
The key to this expression is the line anchor metacharacter ^. You can break the expression down like this:
| Regular Expression | Description |
|---|---|
| ^ | at the start of the line, followed immediately by . . . |
| W | then . . . |
| o | followed by . . . |
| r | then . . . |
| d | and lastly . . . |
| \b | a word boundary. |
The \b character class is an anchor like the ^ line anchor. However, \b is a word anchor, and the ^ metacharacter is a line anchor.
