Finding Four to Six letter words using regular expressions
In this regular expression we are going to find out All Four to Six letter words. A word is a unit (member) of language that represents a concept which can be expressively communicated with meaning. A word consists of one or more morphemes which are linked more or less tightly together, and has a phonetic value. Get more information about Finding Words using regular expressions.
Regular Expression Pattern
(?<=(?:\s|\G|\A))\w{4,6}(?=(?:\s|\Z|\.|\?|\!))
A description of the regular expression:
Match a prefix but exclude it from the capture. [(?:\s|\G|\A)]
Match expression but don’t capture it. [\s|\G|\A]
Select from 3 alternatives
Whitespace
Beginning of current search
Beginning of string
Alphanumeric, between 4 and 6 repetitions
Match a suffix but exclude it from the capture. [(?:\s|\Z|\.|\?|\!)]
Match expression but don’t capture it. [\s|\Z|\.|\?|\!]
Select from 5 alternatives
Whitespace
End of string or before new line at end of string
Literal .
Literal ?
Literal !
How It Works
This regular expression will find All Four to Six letter words. Here we first check word start with Whitespace, Beginning of current search, Beginning of string. then we check Alphanumeric value, between 4 and 6 repetitions (means Four to Six letter words) ending with Whitespace or End of string or before new line at end of string or Literal . or Literal ? or Literal !
ASP.NET
<%@ Page Language="VB" AutoEventWireup="false" CodeFile="Default.aspx.vb" Inherits="_Default" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
<title>Four to Six letter words using regular expressions</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<span>Valid Format: enter an Alphanumeric Four to Six letter words</span><br />
<asp:TextBox id="txtInput" runat="server"></asp:TextBox><br />
<asp:RegularExpressionValidator Id="vldRejex" RunAt="server" ControlToValidate="txtInput" ErrorMessage="Please input an Alphanumeric Four to Six letter words" ValidationExpression="^(?<=(?:\s|\G|\A))\w{4,6}(?=(?:\s|\Z|\.|\?|\!))$">
</asp:RegularExpressionValidator><br />
<asp:Button Id="btnSubmit" RunAt="server" CausesValidation="True" Text="Submit"></asp:Button>
</div>
</form>
</body>
</html>
C#.NET
//use System.Text.RegularExpressions befour using this function
public bool vldRegex(string strInput)
{
//create Regular Expression Match pattern object
Regex myRegex = new Regex("^(?<=(?:\\s|\\G|\\A))\\w{4,6}(?=(?:\\s|\\Z|\\.|\\?|\\!))$");
//boolean variable to hold the status
bool isValid = false;
if (string.IsNullOrEmpty(strInput))
{
isValid = false;
}
else
{
isValid = myRegex.IsMatch(strInput);
}
//return the results
return isValid;
}
VB.NET
‘Imports System.Text.RegularExpressions befour using this function
Public Function vldRegex(ByVal strInput As String) As Boolean
‘create Regular Expression Match pattern object
Dim myRegex As New Regex("^(?<=(?:\s|\G|\A))\w{4,6}(?=(?:\s|\Z|\.|\?|\!))$")
‘boolean variable to hold the status
Dim isValid As Boolean = False
If strInput = "" Then
isValid = False
Else
isValid = myRegex.IsMatch(strInput)
End If
‘return the results
Return isValid
End Function





Comments
No comments yet.
Leave a comment