CIS 451 Regular Expressions Dr Ralph D Westfall

  • Slides: 15
Download presentation
CIS 451: Regular Expressions Dr. Ralph D. Westfall January, 2009

CIS 451: Regular Expressions Dr. Ralph D. Westfall January, 2009

Regular Expressions n find patterns in text strings based on the idea of wild

Regular Expressions n find patterns in text strings based on the idea of wild cards n n . e. g. , dir s*. doc in DOS/Windows finds any. doc file starting with s, followed by any other character(s) find much more complicated patterns n n e. g. , any one of a specified list of characters rather than just one or any e. g. , any character except ones in a list

Reg. Ex Class n n implements the concept of regular expressions in. NET languages

Reg. Ex Class n n implements the concept of regular expressions in. NET languages could be used for E-commerce for input validation n checking user inputs such as e-mail addresses, credit card #s, telephone #s, etc. to see if they match the typical patterns

Regular Expression Object n create a regular expression object Dim reg as Regex reg

Regular Expression Object n create a regular expression object Dim reg as Regex reg = New Regex("[pattern]") n could also create Regex object without a pattern, and then provide the pattern as an argument in a Regex method

. NET Reg. Ex Patterns. ba. finds bat, bal, bac (matches any single character)

. NET Reg. Ex Patterns. ba. finds bat, bal, bac (matches any single character) ? bottles? finds bottle and bottles (0 or 1 of the character before ? ) [3 -9] finds 3, 4, … 9 (any 1 character in specified range) + to+ finds to, too (1 or more of before +) {#} b{2} finds any bb (# of preceding matches together)

. NET Reg. Ex Patterns - 2 | or operator e. g. , a|b

. NET Reg. Ex Patterns - 2 | or operator e. g. , a|b finds a or b escape character to identify literals e. g. , . n can combine different parameters [a-z]{3} finds any 3 letter word [0 -9]{4}( |-)? finds any 4 #s, followed by 0 or 1 occurrence of space or dash (could use in credit card validation)

Regular Expression Object - 2 n methods n reg. Is. Match([string to search]) n

Regular Expression Object - 2 n methods n reg. Is. Match([string to search]) n n reg. Match([string to search]) n n returns True if pattern is found in string returns the matching characters in a string each time pattern is found reg. Replace([string 1, string 2]) n replaces every occurrence of string 1 with string 2 'notes (here =. aspx)

Using Regular Expressions Dim ok as Boolean Dim reg As New Regex("[0 -9]{4}( |-)?

Using Regular Expressions Dim ok as Boolean Dim reg As New Regex("[0 -9]{4}( |-)? " & _ "[0 -9]{4}( |-)? [0 -9]{4}", _ Regex. Options. Compiled) ok = reg. Is. Match("1234 -4323 -9876 -6543") If ok Then Response. Write "String is OK" End If 'notes (here =. html)

Warning n a regular expression may say a String is OK even if there

Warning n a regular expression may say a String is OK even if there are other characters around it e. g. , Dim input as String Dim ok as Boolean input = "XYZ 1234 -4323 -9876 -6543 XYZ" ok = reg. Is. Match(input) 'credit card If ok Then 'pattern Msg. Box(input & " is OK") End If

Extracting Matches from Strings n can use. Match() function to separate matching part from

Extracting Matches from Strings n can use. Match() function to separate matching part from other characters around it e. g. , Dim input as String input = "XYZ 1234 -4323 -9876 -6543 XYZ" If reg. Is. Match(input) Then Msg. Box(reg. Match(input) & " is OK") End If

More Regular Expressions Info n n Using Regular Expressions with. NET Regular Expressions Reference

More Regular Expressions Info n n Using Regular Expressions with. NET Regular Expressions Reference Google search (5+ million occurrences) history of regular expressions n n concepts date back to 1940 s (before computers) T-shirt

Reg. Ex Exercises 1 explain the following e-mail address pattern [a-z]+@[a-z]+. com 2 extend

Reg. Ex Exercises 1 explain the following e-mail address pattern [a-z]+@[a-z]+. com 2 extend it to handle following endings: n 3 com, edu, org, net, gov, mil, int (must appear at least once) modify it to allow numbers, dashes [-] and periods [. ] after the 1 st character

Reg. Ex Exercises - 2 n create patterns to validate n zip codes both

Reg. Ex Exercises - 2 n create patterns to validate n zip codes both as 5 -digit and Zip + 4 n n n e. g. , 90702 or 90702 -7934 phone #s (intnl, long distance, and local) credit card numbers names (including middle initial, von, de, etc. ) course #s (CIS, EBZ, CS; 1 xx-4 xx, etc. )

Reg. Ex Exercises - 3 n n test text or files samples against regular

Reg. Ex Exercises - 3 n n test text or files samples against regular expressions at Regular Expression Library's tester page and report back test regular expressions n n n from previous pages in this Power. Point others that you make up from Regular Expression Library

Reg. Ex Exercises - 4 n n n use Regular Expression Library's tester page

Reg. Ex Exercises - 4 n n n use Regular Expression Library's tester page to Load a Data Source from a URL and find data in the web page using a regular expression that you specify OR find free (trial) software that will do the same thing