save up to 40%

How to use the Regex class in a regular expression?

Are you preparing for IT certification? With practice questions, study notes, interactive quizzes, tips and technical articles, uCertify PrepKits ensure that you get a solid grasp of core technical concepts to ace your certification exam in first attempt.

How to use the Regex class in a regular expression?

Rating:

Take the following steps to use the Regex class in a regular expression :

  1. Implement the following namespace:

    using System.Text.RegularExpressions;


  2. Create a Regex class object as follows:

    Regex reg = new Regex(@"\b(?<word>\w+)\s+(\k<word>)\b",
    RegexOptions.Compiled | RegexOptions.IgnoreCase);
    //Regular expression to check duplicate values in the string;


  3. Create a string object and initialize it as follows:

    string text = "This is uCertify.com Prepkit questions Prepkit uCertify and uCertify";

  4. Create a MatchCollection object to find a match as follows:

    MatchCollection matches = reg.Matches(text);

  5. Display the number of matches found as follows:

    Console.WriteLine("No. Of Matches Found -", matches.Count);

  6. Display information about each match found as follows:

    foreach (Match match in matches)
    {
    string myword = match.Groups["word"].Value;
    int i = match.Index;
    Console.WriteLine("{0} Repeated At The Position {1}", myword, i);
    }


Rating:



Other articles

Click here to Article home

 
uCertify.com | Our Company | Articles | Privacy | Security | Contact Us | News and Press Release | uCertify India
MCSE: MCSA, MCTS, MCITP    JAVA Certification: SCJP, SCWCD Cisco Certification: CCNA, CCENT, A+, Network+, Security+
Oracle Certification: OCP 9i, OCP 10g, OCA 9i, OCA 10g CIW foundation    EC-212-32    CISSP    Photoshop ACE    Adobe Flash ACE
© 2008 uCertify.com. All rights reserved. All trademarks are the property of their respective owners.