Using Regular Expression for pattern search and regular expression validator

Standard
   Regular expression is the single or group of special symbols to construct pattern to find data from the contents and allow or restrict the input data to the system. Regular Expression just like separate computer language. .Net has this feature to reduce the code to achieve above functionalities. System.Text.RegularExpressions gives this feature.

Symbol

Meaning

Example

Matches

^

Beginning of input text

^A

A, Text started with A (Represent to find the first word in the multiline text.)

$

End of the input text

Z$

Z, Text ended with Z (Represent to find the last word in the multiline text.)

.

Any single character except new line character (\n).

Li.e

Line, Lime

*

Preceding character may be repeated 0 or more times.

is*e

ie, ise, isse, issse, isssse …

+

Preceding character may be repeated 1 or more times.

is+e

ise, isse, issse, isssse …

?

Preceding character may be repeated 0 or 1 time.

is?e

ie, ise

\s

Any whitespace character

\sar

[Space]ar, [new line\n]ar [tab\t]ar

\S

Any character other than whitespace character.

\Sar

Ear, jar, war, var ..

\b

Any word boundary (either beginning or end) characters specified

ion\b

Nation, solution, addition …

\B

Any word with specified characters in any position except (boundary) beginning and end.

\Bcc

Success, access

\d

Decimal characters

\d{3}

3 digit

\D

Non -Decimal characters

(\D)*

Abcdefgh

  • Regularexpression uses to validate the input data in the specific format. In .Net web pages validate the input text using regular expression validator control (System.Web.UI.WebControls.RegularExpressionValidator).

Data

Pattern

Example

Email

\w+([-+.’]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*

thirumalganesh@hotmail.com

US telephone

((\(\d{3}\) ?)|(\d{3}-))?\d{3}-\d{4}

(656) 555-7419

Singapore telephone with country code

((\(\d{2}\) ?))?\d{8}

(65) 65557419

Web address

http(s)?://([\w-]+\.)+[\w-]+(/[\w- ./?%&=]*)?

http://www.microsoft.com/

Numerical input only

(\d{1})*

123456789

Non numerical input

(\D)*

abcdefgh

  • uses to search the words in specific pattern from the contents.

using System;

using System.Text.RegularExpressions;

namespace RegularExpression

{

class Program

{

public static string input = @"I have been in software industry

since 1999 holding various roles and responsibilities as developer,

tester and support team member from small scale industry to industry

giant. And having experience and expertise with Microsoft technologies

such as VB, SQL Server, C#.Net, Vb.NET, ASP.NET, ADO.NET, Crystal report

and Oracle. And involved in requirement study, design, development,

testing, implementation, documentation , training and support in my carrier.";

static void Main(string[] args)

{

//Some example for the pattern to search the word from the input text

//@"\S*\Bcr\S*" Microsoft

//@"\bcr\S*" Crystal

//@"\S*\bvb\S*" Vb, vb.net

//@"\S*Net\b*" C#.Net, VB.Net, ASP.Net,

//@"\S*ier.$" carrier. (last word in the input string)

//@"^i\S*" I (First word in the input string)

MatchCollection Matches = findTheMatches(@"^i\S*");

foreach (Match match in Matches)

{

Console.WriteLine(match);

}

Console.WriteLine(input);

Console.ReadLine();

}

static MatchCollection findTheMatches(string Pattern)

{

return Regex.Matches(input, Pattern, RegexOptions.IgnoreCase);

}

}

}

  • delete the repeated word from the contents using specific pattern

  • convert the character case in the word using specific pattern.

  • make sure the character case in the word.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s