Download presentation
Presentation is loading. Please wait.
Published byNoah Miller Modified over 9 years ago
1
C# Regular Expressions C#.Net Software Development Version 1.0
2
Copyright © 2007-2011 by Dennis A. Fairclough all rights reserved 2 Introducing Regular Expressions String pattern matching tool String pattern matching tool Regular expressions constitute a language Regular expressions constitute a language C# regular expressions are classes in the languageC# regular expressions are classes in the language Used in many programming languages Used in many programming languages CNS 3240: Computational Theory CNS 3240: Computational Theory Discusses the theory of Regular ExpressionsDiscusses the theory of Regular Expressions
3
Copyright © 2007-2011 by Dennis A. Fairclough all rights reserved 3 Pattern Matching Match any single character in the bracketed set (range) Match any single character in the bracketed set (range) [a-zA-Z][a-zA-Z] Anything not in brackets (range) is matched exactly (spaces) Anything not in brackets (range) is matched exactly (spaces) Except for special characters (esc with \ )Except for special characters (esc with \ ) abc[a-zA-Z]abc[a-zA-Z] * Match the preceding pattern (range) zero or more times (Kleen Star) * Match the preceding pattern (range) zero or more times (Kleen Star) [a-zA-Z]*[a-zA-Z]* + Match the preceding pattern (range) one or more times + Match the preceding pattern (range) one or more times [a-zA-Z]+[a-zA-Z]+
4
Copyright © 2007-2011 by Dennis A. Fairclough all rights reserved 4 Language Elements ( )pattern group ( )pattern group |“or”, choose between patterns |“or”, choose between patterns [ ]defines a range of characters [ ]defines a range of characters [A-Z]upper case character range [A-Z]upper case character range { }used as a quantifier { }used as a quantifier \escape character \escape character. matches any character. matches any character ^beginning of line ^beginning of line $end of line $end of line [^]not character range [^]not character range
5
Copyright © 2007-2011 by Dennis A. Fairclough all rights reserved 5 Quantifiers *Matches zero or more *Matches zero or more +Matches one or more +Matches one or more ?Matches zero or one ?Matches zero or one {n}Matches exactly n {n}Matches exactly n {n,}Matches at least n {n,}Matches at least n {n,m}Matches at least n, up to m {n,m}Matches at least n, up to m Greedy quantifiers always span the largest pattern they can match Greedy quantifiers always span the largest pattern they can match Lazy quantifiers always take the smallest pattern they can match Lazy quantifiers always take the smallest pattern they can match The lazy quantifiers “?”The lazy quantifiers “?” Beware of spaces! Beware of spaces!
6
Copyright © 2007-2011 by Dennis A. Fairclough all rights reserved 6 Character Classes \wMatches any word character \wMatches any word character Same as: [a-zA-Z_0-9]Same as: [a-zA-Z_0-9] \WMatches any non-word character \WMatches any non-word character Same as: [^a-zA-Z_0-9]Same as: [^a-zA-Z_0-9] \sMatches any white-space character \sMatches any white-space character Same as: [ \f\n\r\t\v]Same as: [ \f\n\r\t\v] \SMatches any non-white-space character \SMatches any non-white-space character Same as: [^ \f\n\r\t\v]Same as: [^ \f\n\r\t\v] \dMatches any digit character \dMatches any digit character Same as: [0-9]Same as: [0-9] \DMatches any non-digit character \DMatches any non-digit character Same as: [^0-9]Same as: [^0-9] space
7
Character \* \+ \? \( \) \| \\ \! \. \[ \] \^ \{ \} \$ \# Escaping Characters Ordinary characters, other than the ones in the table below, match themselves. That is, an "a" in a regular expression specifies that input should match an "a“. 7 Copyright © 2007-2011 by Dennis A. Fairclough all rights reserved Regular expressions have the following metacharacters, which have a special rather than literal meaning: \ * + ? | { [ ( ) ^ $. #
8
Copyright © 2007-2011 by Dennis A. Fairclough all rights reserved 8 ^ has two context-dependent meanings: an anchor and a character class negator. $ has two context-dependent meanings: an anchor and a replacement group denoter. Anchors The anchors ^ and $ match a particular position. By default: ^ Matches the start of the string. $ Matches the end of the string.
9
Escaped Character Description ordinary characters Characters other than. $ ^ { [ ( | ) * + ? \ match themselves. \a Matches a bell (alarm) \u0007. \b Matches a backspace \u0008 if in a [] character class; otherwise, see the note following this table.character class \t Matches a tab \u0009. \r Matches a carriage return \u000D. \v Matches a vertical tab \u000B. \f Matches a form feed \u000C. \n Matches a new line \u000A. \e Matches an escape \u001B. \040 Matches an ASCII character as octal (up to three digits); numbers with no leading zero are backreferences if they have only one digit or if they correspond to a capturing group number. (For more information, see Backreferences.) For example, the character \040 represents a space.Backreferences \x20 Matches an ASCII character using hexadecimal representation (exactly two digits). \cC Matches an ASCII control character; for example, \cC is control-C. \u0020 Matches a Unicode character using hexadecimal representation (exactly four digits). \ When followed by a character that is not recognized as an escaped character, matches that character. For example, \* is the same as \x2A. Note The escaped character \b is a special case. In a regular expression, \b denotes a word boundary (between \w and \W characters) except within a [] character class, where \b refers to the backspace character. In a replacement pattern, \b always denotes a backspace. 9 Copyright © 2007-2011 by Dennis A. Fairclough all rights reserved
10
Character Description of Substitution $number Substitutes the last substring matched by group number number (decimal). ${name} Substitutes the last substring matched by a (? ) group. $$ Substitutes a single "$" literal. $& Substitutes a copy of the entire match itself. $` Substitutes all the text of the input string before the match. $' Substitutes all the text of the input string after the match. $+ Substitutes the last group captured. $_ Substitutes the entire input string. 10 Copyright © 2007-2011 by Dennis A. Fairclough all rights reserved
11
Character classDescription.Matches any character except \n. If modified by the Singleline option, a period character matches any character. For more information, see Regular Expression Options.Regular Expression Options [aeiou]Matches any single character included in the specified set of characters. [^aeiou]Matches any single character not in the specified set of characters. [0-9a-fA-F]Use of a hyphen (–) allows specification of contiguous character ranges. \p{name}Matches any character in the named character class specified by {name}. Supported names are Unicode groups and block ranges. For example, Ll, Nd, Z, IsGreek, IsBoxDrawing. \P{name}Matches text not included in groups and block ranges specified in {name}. \wMatches any word character. Equivalent to the Unicode character categories [\p{Ll}\p{Lu}\p{Lt}\p{Lo}\p{Nd}\p{Pc}]. If ECMAScript-compliant behavior is specified with the ECMAScript option, \w is equivalent to [a-zA-Z_0-9].ECMAScript option \WMatches any nonword character. Equivalent to the Unicode categories [^\p{Ll}\p{Lu}\p{Lt}\p{Lo}\p{Nd}\p{Pc}]. If ECMAScript-compliant behavior is specified with the ECMAScript option, \W is equivalent to [^a-zA-Z_0-9]. \sMatches any white-space character. Equivalent to the Unicode character categories [\f\n\r\t\v\x85\p{Z}]. If ECMAScript-compliant behavior is specified with the ECMAScript option, \s is equivalent to [ \f\n\r\t\v]. \SMatches any non-white-space character. Equivalent to the Unicode character categories [^\f\n\r\t\v\x85\p{Z}]. If ECMAScript-compliant behavior is specified with the ECMAScript option, \S is equivalent to [^ \f\n\r\t\v]. \dMatches any decimal digit. Equivalent to \p{Nd} for Unicode and [0-9] for non-Unicode, ECMAScript behavior. \DMatches any nondigit. Equivalent to \P{Nd} for Unicode and [^0-9] for non-Unicode, ECMAScript behavior. 11 Copyright © 2007-2011 by Dennis A. Fairclough all rights reserved
12
AssertionDescription ^Specifies that the match must occur at the beginning of the string or the beginning of the line. For more information, see the Multiline option in Regular Expression Options.Regular Expression Options $Specifies that the match must occur at the end of the string, before \n at the end of the string, or at the end of the line. For more information, see the Multiline option in Regular Expression Options. Regular Expression Options \ASpecifies that the match must occur at the beginning of the string (ignores the Multiline option). \ZSpecifies that the match must occur at the end of the string or before \n at the end of the string (ignores the Multiline option). \zSpecifies that the match must occur at the end of the string (ignores the Multiline option). \GSpecifies that the match must occur at the point where the previous match ended. When used with Match.NextMatch(), this ensures that matches are all contiguous. \bSpecifies that the match must occur on a boundary between \w (alphanumeric) and \W (nonalphanumeric) characters. The match must occur on word boundaries — that is, at the first or last characters in words separated by any nonalphanumeric characters. \BSpecifies that the match must not occur on a \b boundary. 12 Copyright © 2007-2011 by Dennis A. Fairclough all rights reserved
13
QuantifierDescription *Specifies zero or more matches; for example, \w* or (abc)*. Equivalent to {0,}. +Specifies one or more matches; for example, \w+ or (abc)+. Equivalent to {1,}. ?Specifies zero or one matches; for example, \w? or (abc)?. Equivalent to {0,1}. {n}Specifies exactly n matches; for example, (pizza){2}. {n,}Specifies at least n matches; for example, (abc){2,}. {n,m}Specifies at least n, but no more than m, matches. *?Specifies the first match that consumes as few repeats as possible (equivalent to lazy *). +?Specifies as few repeats as possible, but at least one (equivalent to lazy +). ??Specifies zero repeats if possible, or one (lazy ?). {n}?Equivalent to {n} (lazy {n}). {n,}?Specifies as few repeats as possible, but at least n (lazy {n,}). {n,m}?Specifies as few repeats as possible between n and m (lazy {n,m}). 13 Copyright © 2007-2011 by Dennis A. Fairclough all rights reserved
14
Grouping ConstructDescription ( )Captures the matched substring (or noncapturing group; for more information, see the ExplicitCapture option in Regular Expression Options). Captures using () are numbered automatically based on the order of the opening parenthesis, starting from one. The first capture, capture element number zero, is the text matched by the whole regular expression pattern.Regular Expression Options (? )Captures the matched substring into a group name or number name. The string used for name must not contain any punctuation and it cannot begin with a number. You can use single quotes instead of angle brackets; for example, (?'name'). (? )Balancing group definition. Deletes the definition of the previously defined group name2 and stores in group name1 the interval between the previously defined name2 group and the current group. If no group name2 is defined, the match backtracks. Because deleting the last definition of name2 reveals the previous definition of name2, this construct allows the stack of captures for group name2 to be used as a counter for keeping track of nested constructs such as parentheses. In this construct, name1 is optional. You can use single quotes instead of angle brackets; for example, (?'name1-name2'). (?: )Noncapturing group. (?imnsx-imnsx: )Applies or disables the specified options within the subexpression. For example, (?i-s: ) turns on case insensitivity and disables single-line mode. For more information, see Regular Expression Options. Regular Expression Options (?= )Zero-width positive lookahead assertion. Continues match only if the subexpression matches at this position on the right. For example, \w+(?=\d) matches a word followed by a digit, without matching the digit. This construct does not backtrack. (?! )Zero-width negative lookahead assertion. Continues match only if the subexpression does not match at this position on the right. For example, \b(?!un)\w+\b matches words that do not begin with un. (?<= )Zero-width positive lookbehind assertion. Continues match only if the subexpression matches at this position on the left. For example, (?<=19)99 matches instances of 99 that follow 19. This construct does not backtrack. (?<! )Zero-width negative lookbehind assertion. Continues match only if the subexpression does not match at the position on the left. (?> )Nonbacktracking subexpression (also known as a "greedy" subexpression). The subexpression is fully matched once, and then does not participate piecemeal in backtracking. (That is, the subexpression matches only strings that would be matched by the subexpression alone.) 14 Copyright © 2007-2011 by Dennis A. Fairclough all rights reserved
15
NumberNamePattern 00 (default name) ((? abc)/d+)?(? xyz)(.*) 11 (default name) ((? abc)/d+) 22 (default name) (.*) 3One(? abc) 4Two(? xyz) 15 Copyright © 2007-2011 by Dennis A. Fairclough all rights reserved
16
Backreference constructDefinition \numberBackreference. For example, (\w)\1 finds doubled word characters. \k Named backreference. For example, (? \w)\k finds doubled word characters. The expression (? \w)\43 does the same. You can use single quotes instead of angle brackets; for example, \k'char'. 16 Copyright © 2007-2011 by Dennis A. Fairclough all rights reserved
17
Alternation constructDefinition |Matches any one of the terms separated by the | (vertical bar) character; for example, cat|dog|tiger. The leftmost successful match wins. Beware of spaces! (?(expression)yes|no)Matches the "yes" part if the expression matches at this point; otherwise, matches the "no" part. The "no" part can be omitted. The expression can be any valid subexpression, but it is turned into a zero-width assertion, so this syntax is equivalent to (?(?=expression)yes|no). Note that if the expression is the name of a named group or a capturing group number, the alternation construct is interpreted as a capture test (described in the next row of this table). To avoid confusion in these cases, you can spell out the inside (?=expression) explicitly. (?(name)yes|no)Matches the "yes" part if the named capture string has a match; otherwise, matches the "no" part. The "no" part can be omitted. If the given name does not correspond to the name or number of a capturing group used in this expression, the alternation construct is interpreted as an expression test (described in the preceding row of this table). 17 Copyright © 2007-2011 by Dennis A. Fairclough all rights reserved
18
ConstructDefinition (?imnsx-imnsx)Sets or disables options such as case insensitivity to be turned on or off in the middle of a pattern. For information on specific options, see Regular Expression Options. Option changes are effective until the end of the enclosing group. See also the information on the grouping construct (?imnsx-imnsx: ), which is a cleaner form.Regular Expression Optionsgrouping construct (?# )Inline comment inserted within a regular expression. The comment terminates at the first closing parenthesis character. # [to end of line]X-mode comment. The comment begins at an unescaped # and continues to the end of the line. (Note that the x option or the RegexOptions.IgnorePatternWhitespace enumerated option must be activated for this kind of comment to be recognized.) 18 Copyright © 2007-2011 by Dennis A. Fairclough all rights reserved
19
\b[A-Za-z0-9._%-]+@[A-Za-z0-9.-]+\.(com|org|biz|net|info)\b Email Address.*\.txt File extensions ]*>(.*?) HTML Tags \b\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}\b IP Addresses or \b(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0- 9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\b IP Addresses [-+]?([0-9]*\.[0-9]+) Floating Point Number (19|20)\d\d[- /.](0[1-9]|1[012])[- /.](0[1-9]|[12][0-9]|3[01]) Dates (19|20)\d\d or (0[1-9]|1[012])[- /.](0[1-9]|[12][0-9]|3[01])[- /.](19|20)\d\d Dates [ \t\r\n\f\v] or [\s] White space //.*$Single Line Comment /\*.*?\*/ C Style Comment "[^"\\\r\n]*(\\.[^"\\\r\n]*)*" Single Line String "[^"\\]*(\\.[^"\\]*)*" Multiline String \b\d+\b Positive Integer [-+]?\b\d+\b Signed Positive Integer ((\b[0-9]+)?\.)?[0-9]+\b Integer & Float (\b[0-9]+\.([0-9]+\b)?|\.[0-9]+\b) Matches a Float \b(first|second|third|etc)\b Matches Reserved Words 19 Copyright © 2007-2011 by Dennis A. Fairclough all rights reserved
20
enum INSCOPE { // common input scopes IS_DEFAULT = 0, IS_URL = 1, IS_FILE_FULLFILEPATH = 2, IS_FILE_FILENAME = 3, IS_EMAIL_USERNAME = 4, IS_EMAIL_SMTPEMAILADDRESS = 5, IS_LOGINNAME = 6, IS_PERSONALNAME_FULLNAME = 7, IS_PERSONALNAME_PREFIX = 8, IS_PERSONALNAME_GIVENNAME = 9, IS_PERSONALNAME_MIDDLENAME = 10, IS_PERSONALNAME_SURNAME = 11, IS_PERSONALNAME_SUFFIX = 12, IS_ADDRESS_FULLPOSTALADDRESS = 13, IS_ADDRESS_POSTALCODE = 14, IS_ADDRESS_STREET = 15, IS_ADDRESS_STATEORPROVINCE = 16, IS_ADDRESS_CITY = 17, IS_ADDRESS_COUNTRYNAME = 18, IS_ADDRESS_COUNTRYSHORTNAME = 19, IS_CURRENCY_AMOUNTANDSYMBOL = 20, IS_CURRENCY_AMOUNT = 21, IS_DATE_FULLDATE = 22, IS_DATE_MONTH = 23, IS_DATE_DAY = 24, IS_DATE_YEAR = 25, IS_DATE_MONTHNAME = 26, IS_DATE_DAYNAME = 27, IS_DIGITS = 28, IS_NUMBER = 29, IS_ONECHAR = 30, IS_PASSWORD = 31, IS_TELEPHONE_FULLTELEPHONENUMBER = 32, IS_TELEPHONE_COUNTRYCODE = 33, IS_TELEPHONE_AREACODE = 34, IS_TELEPHONE_LOCALNUMBER = 35, IS_TIME_FULLTIME = 36, IS_TIME_HOUR = 37, IS_TIME_MINORSEC = 38, IS_NUMBER_FULLWIDTH = 39, IS_ALPHANUMERIC_HALFWIDTH = 40, IS_ALPHANUMERIC_FULLWIDTH = 41, IS_CURRENCY_CHINESE = 42, IS_BOPOMOFO = 43, IS_HIRAGANA = 44, IS_KATAKANA_HALFWIDTH = 45, I iS_KATAKANA_FULLWIDTH = 46,. IS_HANJA = 47, IS_PHRASELIST = -1, IS_REGULAREXPRESSION = -2, I S_SRGS = -3, IS_XML = -4, } 20 Copyright © 2007-2011 by Dennis A. Fairclough all rights reserved
21
21 Putting It Together Regular Expression for C# identifiers: Regular Expression for C# identifiers: [a-zA-Z_][a-zA-Z0-9_]* (_ _ reserved)[a-zA-Z_][a-zA-Z0-9_]* (_ _ reserved) Floating Point Numbers: Floating Point Numbers: (0|([1-9][0-9]*))?\.[0-9]+(0|([1-9][0-9]*))?\.[0-9]+ C# Hexidecimal numbers C# Hexidecimal numbers 0[xX][0-9a-fA-F]+0[xX][0-9a-fA-F]+
22
Copyright © 2007-2011 by Dennis A. Fairclough all rights reserved 22 C# RE Classes System.Text.RegularExpressions System.Text.RegularExpressions RegexRegex MatchMatch MatchCollectionMatchCollection CaptureCapture CaptureCollectionCaptureCollection GroupGroup
23
Copyright © 2007-2011 by Dennis A. Fairclough all rights reserved 23 Regex Class Static or Instance? Static or Instance? Static: Static: Regex exposes static methods for doing RE matching quicklyRegex exposes static methods for doing RE matching quickly Use only when need to match very few timesUse only when need to match very few times Instance: Instance: Holds a Regular Expression as an objectHolds a Regular Expression as an object Compiles the expression to make it fasterCompiles the expression to make it faster Use when need to match many timesUse when need to match many times
24
Copyright © 2007-2011 by Dennis A. Fairclough all rights reserved 24 Class Diagrams Regex +Match:Match(string) +MatchCollection:Matches(string) Match : Group +GroupCollection:Groups +Match:NextMatch() Group : Capture +CaptureCollection:Captures +bool:Success Capture +string:Value +int:Length +int:Index
25
Copyright © 2007-2011 by Dennis A. Fairclough all rights reserved 25 Matches private Regex re1 = new Regex(@"(([2-9]\d{2})-)?(\d{3})-(\d{4})"); private string input1 = “801-224-6707"; Match: Value (from Capture) Index (from Capture) Length (from Capture) Success (from Group) NextMatch() Captures (from Group) Groups
26
Copyright © 2007-2011 by Dennis A. Fairclough all rights reserved 26 Linked Matches Follow links using NextMatch() Follow links using NextMatch() Last link Success == false Last link Success == false Match1 NextMatch() Success==true Match2 NextMatch() Success==true Match3 NextMatch() Success==true Match4 NextMatch() Success=false Match m = re2.Match(input2); while(m.Success) { // work with m here m = m.NextMatch(); }
27
Copyright © 2007-2011 by Dennis A. Fairclough all rights reserved 27 Groups private Regex re1 = new Regex("(([2-9]\d{2})-)?(\d{3})-(\d{4})"); Group: Value (from Capture) Index (from Capture) Length (from Capture) Success Captures 1 2 0 Groups are defined in perens () Groups are defined in perens () Captures a matching substring for future use Captures a matching substring for future use Captures in a Capture objectCaptures in a Capture object Group 0 represents the entire expression Group 0 represents the entire expression 34
28
Copyright © 2007-2011 by Dennis A. Fairclough all rights reserved 28 Named Groups (? expression) (? expression) Non-capturing group Non-capturing group (?:expression)(?:expression)(@"(?:(?<areaCode>[2-9]\d{2})-)?(?<prefix>\d{3})-(?<lastFour>\d{4}) (See RegexGroups Demo)
29
Copyright © 2007-2011 by Dennis A. Fairclough all rights reserved 29 Regex Members Note: Each method has both a static and instance implementation Note: Each method has both a static and instance implementation Options Options Escape() Escape() GetGroupNames() GetGroupNames() GetGroupNumbers() GetGroupNumbers() GetNameFromNumber() GetNameFromNumber() GetNumberFromName() GetNumberFromName() IsMatch() IsMatch() Match() Match() Matches() Matches() Replace() Replace() Split() Split() Unescape() Unescape()
30
Copyright © 2007-2011 by Dennis A. Fairclough all rights reserved 30 Regex.Match() Regex.IsMatch(string input, string pattern) Regex.IsMatch(string input, string pattern) returns true if input matches pattern at least oncereturns true if input matches pattern at least once Regex.Match(string input, string pattern) Regex.Match(string input, string pattern) Returns a Match objectReturns a Match object Use Match.Value to get the string value of the matchUse Match.Value to get the string value of the match Regex.Matches(string input, string pattern) Regex.Matches(string input, string pattern) Returns a MatchCollection of all the occurrences of pattern in inputReturns a MatchCollection of all the occurrences of pattern in input
31
Copyright © 2007-2011 by Dennis A. Fairclough all rights reserved 31 Regex Group Name Stuff GetGroupNames() GetGroupNames() Returns all the group names in a string[]Returns all the group names in a string[] GetGroupNumbers() GetGroupNumbers() Returns the group numbers in an int[]Returns the group numbers in an int[] GetNameFromNumber() GetNameFromNumber() GetNumberFromName() GetNumberFromName()
32
Copyright © 2007-2011 by Dennis A. Fairclough all rights reserved 32 Regex.Escape() If you’re not sure what needs to be escaped? If you’re not sure what needs to be escaped? Regex.Escape(string pattern) Regex.Escape(string pattern) Returns a new string with the necessary characters escapedReturns a new string with the necessary characters escaped Need to undo it? Need to undo it? Regex.Unescape(string pattern) Regex.Unescape(string pattern) Returns a new string with all escape characters removedReturns a new string with all escape characters removed
33
Copyright © 2007-2011 by Dennis A. Fairclough all rights reserved 33 Regex.Replace Refer to a group capture in the regex using a $ Refer to a group capture in the regex using a $ Replace(string input, string replacement, int count) Replace(string input, string replacement, int count) string input2 = "aaabbbccc:aaabbbccc:aaabbbccc"; Regex re2 = new Regex("(aaa)(bbb)(ccc)"); Console.WriteLine();Console.WriteLine("Replace..."); Console.WriteLine(re2.Replace(input2, "$3$2$1", 1)); Console.ReadLine();
34
Copyright © 2007-2011 by Dennis A. Fairclough all rights reserved 34 Tokenizing a string Tokenizing means splitting into parts Tokenizing means splitting into parts Why not use String.Split() ?Why not use String.Split() ? Leaves empty strings sometimes Leaves empty strings sometimes We must know all possible tokens (delimiters) We must know all possible tokens (delimiters) Instead of knowing valid charactersInstead of knowing valid characters (See SearchingStrings Demo)
35
Copyright © 2007-2011 by Dennis A. Fairclough all rights reserved 35 Tokenizing using IndexOf() Exactly what we want Exactly what we want No Extra Empty stringsNo Extra Empty strings We only need to know what is a valid characterWe only need to know what is a valid character Not the most understandable code...Not the most understandable code... (See SearchingStrings Demo)
36
Copyright © 2007-2011 by Dennis A. Fairclough all rights reserved 36 Regex.Split() Splits the string on a Regular Expression Pattern Splits the string on a Regular Expression Pattern string input = "one%two%%three%%four"; Console.WriteLine(); Console.WriteLine("Split..."); Console.WriteLine(string.Join(",", Regex.Split(input, @"[%]+"))); Console.ReadLine(); (See SearchingStrings Demo)
37
Copyright © 2007-2011 by Dennis A. Fairclough all rights reserved 37 The Bottom Line Regular expressions are powerful Regular expressions are powerful They are the industry standard for string pattern matching They are the industry standard for string pattern matching They are supported in most language libraries They are supported in most language libraries They are supported in many search tools (grep, as well as VS 2005 search) They are supported in many search tools (grep, as well as VS 2005 search)
38
What did you learn? ?? ?? Copyright © 2007-2011 by Dennis A. Fairclough all rights reserved 38
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.