-----------------------------------------------------------
New Message on BDOTNET
-----------------------------------------------------------
From: MSFT_Saurabh
Message 7 in Discussion
Hi ! Elan,
Here is the sample code to use Regular Expressions: Using the RegExp Object :
Function RegExpTest(patrn, strng)
Dim regEx, Match, Matches ' Create variable.
Set regEx = New RegExp ' Create a regular expression.
regEx.Pattern = patrn ' Set pattern.
regEx.IgnoreCase = True ' Set case insensitivity.
regEx.Global = True ' Set global applicability.
Set Matches = regEx.Execute(strng) ' Execute search.
For Each Match in Matches ' Iterate Matches collection.
RetStr = RetStr & "Match found at position "
RetStr = RetStr & Match.FirstIndex & ". Match Value is '"
RetStr = RetStr & Match.Value & "'." & vbCRLF
Next
RegExpTest = RetStr
End Function
MsgBox(RegExpTest("is.", "IS1 is2 IS3 is4")) Hand-on Lab Using Regular Expressions
to Match a Pattern : Open Visual Studio .NET. Create a new Visual C#
Console Application. Specify the using keyword on the Text.RegularExpressions
namespace so that you will not be required to qualify declarations in those namespaces
later in your code. The using statement must be used prior to any other declarations:
using System.Text.RegularExpressions;
Define a new regular expression that will use a pattern match to validate an e-mail
address. The following regular expression is structured to accomplish three things:
Capture the substring before the @ symbol and put that into the "user" group.
Capture the substring after the @ symbol and put that into the "host" group. Make
sure that the first half of the string does not have an @ symbol. Regex emailregex =
new Regex("(?<user>[EMAIL PROTECTED])@(?<host>.+)");
Define a new string containing a valid e-mail address. This provides a default
value if the method's command-line argument is empty: String s = "[EMAIL PROTECTED]";
Check to see if there are any command-line
parameters; if there are, retrieve the first parameter and assign it to the variable
"s". if ( args.Length > 0 ) { s = args[0]; }
Use the Match method to pass in the e-mail address variable and return a new Match
object. The Match object will return regardless of whether any matches were found in
the source string. Match m = emailregex.Match(s);
By examining the Success property, we can decide whether to continue
processing the Match object or to print an error message. If successful, display the
"user" and "host" named groups within the Groups collection of the Match object. if (
m.Success ) { Console.WriteLine("User: " + m.Groups["user"].Value);
Console.WriteLine("Host: " + m.Groups["host"].Value); } else {
Console.WriteLine(s + " is not a valid email address"); } Console.WriteLine();
To keep the console window open after running the
application, add the following lines of code: System.Console.WriteLine("Press Enter to
Continue..."); System.Console.ReadLine(); Build your project. After this
hands-on, I am sure that you will anxious to know more on Regular Expression. So for
that you can go through some of the below given site :
Introduction to Regular Expressions:
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/script56/html/reconIntroductionToRegularExpressions.asp
Regular Expression Language Elements:
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpgenref/html/cpconregularexpressionslanguageelements.asp
Site devoted to Regular Expressions : http://www.regular-expressions.info/dotnet.html
Hope this helps you out. In case not, you can always revert back on the same.
take care...<o:p> </o:p>
Regards,
Saurabh Verma
Machrotech (India) Pvt. Ltd.
Microsoft India Community Star
MCT.NET, MCSD.NET (EA), MCAD.NET, MCDBA
-----------------------------------------------------------
To stop getting this e-mail, or change how often it arrives, go to your E-mail
Settings.
http://groups.msn.com/BDotNet/_emailsettings.msnw
Need help? If you've forgotten your password, please go to Passport Member Services.
http://groups.msn.com/_passportredir.msnw?ppmprop=help
For other questions or feedback, go to our Contact Us page.
http://groups.msn.com/contact
If you do not want to receive future e-mail from this MSN group, or if you received
this message by mistake, please click the "Remove" link below. On the pre-addressed
e-mail message that opens, simply click "Send". Your e-mail address will be deleted
from this group's mailing list.
mailto:[EMAIL PROTECTED]