1 / 15

Regular Expressions in .NET

Regular Expressions in .NET. BY Sandeep Kumar Gampa. Contents. What is Regular Expression? Regex in .NET Regex Language Elements Examples Regular Expression API How to Test regex in .NET Conclusion. What is Regular Expression?.

Télécharger la présentation

Regular Expressions in .NET

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Regular Expressions in .NET BY Sandeep Kumar Gampa.

  2. Contents. • What is Regular Expression? • Regex in .NET • Regex Language Elements • Examples • Regular Expression API • How to Test regex in .NET • Conclusion

  3. What is Regular Expression? • A regular expression (regex or regexp for short) is a special text string for describing a search pattern. • A regular expression is a set of pattern matching rules encoded in a string according to certain syntax rules. • The syntax (language format) described is compliant with extended regular expressions (EREs) defined in IEEE POSIX 1003.2 • Sample Example for Email Id ^[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,4}$

  4. Regex in .NET • Validation in ASP.NET , We use the RegularExpressionValidator control to validate that input Fields. such as Name, Email Id, Urls, Date • Also when certain patterns need to be replaced by a String. Like Regex.Replace(intput, @“Pattern1", “Pattern2”} • To reformatting an input string by re-arranging the order and placement of the elements within the input string Example: If a date is in DD-MM-YYYY format and is to be changed into MM-DD-YYYY format.

  5. Regex Language Elements • Metacharacters - The constructs within regular expressions that have special meaning are referred to as metacharacters - Characters other than . $ ^ { [ ( | ) ] } * + ? \ match themselves. • Character Classes - Character classes are a mini-language within regular expressions, defined by the enclosing hard braces [ ]. Examples: [a-z A-Z 0-9],

  6. Cont… MeataCharacters and their description followed by an example: • ^ Start of a string. ^abc matches are abc, abcdefg, abc123, • $ End of a string. abc$ matches with abc, endsinabc, 123abc • . Any character (except \n newline) • | Alternation. • {...} Explicit quantifier notation. • [...] Explicit set of characters to match. • (...) Logical grouping of part of an expression. • * 0 or more of previous expression. • + 1 or more of previous expression. • ? 0 or 1 of previous expression; also forces minimal matching when an expression might match several strings within a search string. • \ Preceding one of the above, it makes it a literal instead of a special character. Preceding a special matching character, see below.

  7. Cont… • \w Matches any word character. equivalent to [a-zA-Z_0-9]. • \W Matches any nonword character. Equivalent to the Unicode categories equivalent to [^a-zA-Z_0-9]. • \s Matches any white-space character. equivalent to [ \f\n\r\t\v]. • \S Matches any non-white-space character. equivalent to [^ \f\n\r\t\v]. • \d Matches any decimal digit. • \D Matches any nondigit.

  8. Examples Pattern Description • ^\d{5}$ • ^\d{3}-\d{2}-\d{4}$ • ^[01]?[- .]?(\([2-9]\d{2}\)|[2-9]\d{2})[- .]?\d{3}[- .]?\d{4}$ 5 numeric digits, such as a US ZIP code. Validates the format such as 111-11-1111 (Social Security Number) Validates a U.S. phone number.

  9. Regular Expression API • Regular Expression classes found in the System.Text.RegularExpressions namespace. • the main classes we'll want to use are Regex, Match, and MatchCollection. • Regex: Methods and their description IsMatch - Returns true if the regex finds a match in the input string. Match - Returns a Match object if a match is found in the input string. Matches - Returns a MatchCollection object containing any and all matches found in the input string. Replace - Replaces matches in the input string with a given replacement string.

  10. Cont… • Capture: Represents the results from a single subexpression capture. Capture represents one substring for a single successful capture • CaptureCollection : Represents a sequence of capture substrings. CaptureCollection returns the set of captures done by a single capturing group • Group: Group represents the results from a single capturing group. A capturing group can capture zero, one, or more strings in a single match because of quantifiers, so Group supplies a collection of Capture objects. • GroupCollection: Represents a collection of captured groups. GroupCollection returns the set of captured groups in a single match.

  11. How to Test regex in .NET Simple C# Example: • Regex r = new Regex(pattern,    RegexOptions.IgnoreCase | RegexOptions.IgnorePatternWhitespace); Match m = r.Match(inputtext); if(m.Success) { Console.WriteLine(“Matched String “+m.Group()); }else{ Console.WriteLine(“Not Matched “); }

  12. Example2 • Example: describes how to use other classes. • Using System.Text.RegularExpressions; • string text = "One fish two fish red fish blue fish"; • string pat = @"(?<1>\w+)\s+(?<2>fish)\s*"; • // Compile the regular expression. • Regex r = new Regex(pat, RegexOptions.IgnoreCase); • // Match the regular expression pattern against a text string • Match m = r.Match(text); • while (m.Success) • { • // Display the first match and its capture set. • System.Console.WriteLine("Match=[" + m + "]"); • CaptureCollection cc = m.Captures; • foreach (Capture c in cc) • { • System.Console.WriteLine("Capture=[" + c + "]"); • }

  13. Cont… • // Display Group1 and its capture set. • Group g1 = m.Groups[1]; • System.Console.WriteLine("Group1=[" + g1 + "]"); • foreach (Capture c1 in g1.Captures) • { • System.Console.WriteLine("Capture1=[" + c1 + "]"); • } • // Display Group2 and its capture set. • Group g2 = m.Groups[2]; • System.Console.WriteLine("Group2=["+ g2 + "]"); • foreach (Capture c2 in g2.Captures) • { • System.Console.WriteLine("Capture2=[" + c2 + "]"); • } • // Advance to the next match. • m = m.NextMatch(); • }

  14. Conclusion • Regular expressions provide a very powerful way to describe patterns in text, making them an excellent resource for string validation and manipulation. • The .NET Framework provides first-rate support for regular expressions in its System.Text.RegularExpressions namespace and specifically the Regex class found there. References: • http://msdn.microsoft.com/en-us/library/system.text.regularexpressions.groupcollection.aspx • http://www.regular-expressions.info/dotnet.html • http://regexlib.com/CheatSheet.aspx

  15. Thank You.

More Related