Monday, December 30, 2019

C# Regular Expression - Starting and Ending words

In C # programming, regular expression is used to validate user input and to validate the starting of words, we use caret (^) and for ending words, we use dollar ($).

In C#, The Regex class is used to verify the specific character pattern and it offers several methods and properties to parse input string and verify the specific patterns.

Match the beginning of the string:
Caret (^) is used to matches the beginning of the string

     string regExPattern = "^t";

            Regex regex = new Regex(regExPattern);
            Console.WriteLine(regex.IsMatch("tom"));
            // true

            Console.WriteLine(regex.IsMatch("dom"));
            // false

            Console.WriteLine(regex.IsMatch("jon"));
            // false

Match the ending of the string:
Dollar ($) is used to matches the ending of the string

      string regExPattern = @".com$";

            Regex regex = new Regex(regExPattern);
            Console.WriteLine(regex.IsMatch("tom@mail.com"));
            // true

            Console.WriteLine(regex.IsMatch("dom"));
            // false

            Console.WriteLine(regex.IsMatch("jom@mail.com"));
            // true


No comments:

SQL Server - Identify unused indexes

 In this blog, we learn about the index usage information (SYS.DM_DB_INDEX_USAGE_STATS) and analyze the index usage data (USER_SEEKS, USER_S...