Tips n Tracks

  • Increase font size
  • Default font size
  • Decrease font size
  • default color
  • black color

Reference

Sample image

Microsoft .NET Framework Get Details.

Sample image

Microsoft .NET Framework Get Details.

Reference

Sample image Microsoft .NET Framework Get Details.
Sample image

Microsoft .NET Framework Get Details.


Finding words or String with same beginning and end using regular expressions with .net


Finding words or String with same beginning and end using regular expressions with .net

In this regular expression we are going to find words or String with same beginning and end. Means we are going to search words like xerox with same beginning and end "x".

Regular Expression Pattern

\b(?<First>\w{1,})(?<Second>\w+)(?(Second)\k<First>)\b

A description of the regular expression:

  First or last character in a word
  [First]: A named capture group. [\w{1,}]
      Alphanumeric, at least 1 repetitions
  [Second]: A named capture group. [\w+]
      Alphanumeric, one or more repetitions
  Conditional Expression with "Yes" clause only
      Did the capture named [Second] match?
      If yes, search for [\k<First>]
          Backreference to capture named: First
  First or last character in a word

Sucessful Matches

xerox
404
rotor
rotor
ISAPI

How It Works

This regular expression will check for first character of a word, Alphanumeric, at least 1 repetitions and capture it in group called [First]. Than we search for Alphanumeric, one or more repetitions Conditional Expression with "Yes" clause only. Then Backreference to capture named: [First] to finish our Search.

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 runat="server">
<title>Finding all words like xerox with same beginning and end "x" using regular expressions with .net</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<span>Valid Format: enter words like xerox with same beginning and end "x"</span><br />
<asp:TextBox id="txtInput" runat="server"></asp:TextBox><br />
<asp:RegularExpressionValidator Id="vldRejex" RunAt="server" ControlToValidate="txtInput" ErrorMessage="word not have same beginning and end" ValidationExpression="^\b(?<First>\w{1,})(?<Second>\w+)(?(Second)\k<First>)\b$">
</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
//using System.Text.RegularExpressions;
public bool vldRegex(string strInput)
{
//create Regular Expression Match pattern object
Regex myRegex = new Regex("^\\b(?<First>\\w{1,})(?<Second>\\w+)(?(Second)\\k<First>)\\b$");
//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("^\b(?<First>\w{1,})(?<Second>\w+)(?(Second)\k<First>)\b$")
‘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

 

Chetan love blogging. He regularly blogs at http://www.tipsntracks.com. You can connect with Chetan on Twitter, Facebook and Google Plus...

If you enjoyed this post, please consider to leave a comment or subscribe to the feed and get future articles delivered to your feed reader.

Comments

[...] Finding words or String with same beginning and end using regular expressions with .net [...]

Pingback by Text and Data Manipulation with Regular Expressions in .NET Development | Tips n Tracks on November 21, 2009 @ 11:23 am

Leave a comment

(required)

(required)

*
To prove that you're not a bot, enter this code
Anti-Spam Image