• Like
MELJUN CORTES Visual Bsic 2005  String
Upcoming SlideShare
Loading in...5
×

MELJUN CORTES Visual Bsic 2005 String

  • 60 views
Uploaded on

MELJUN CORTES Visual Bsic 2005 String

MELJUN CORTES Visual Bsic 2005 String

More in: Technology
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Be the first to comment
    Be the first to like this
No Downloads

Views

Total Views
60
On Slideshare
0
From Embeds
0
Number of Embeds
0

Actions

Shares
Downloads
0
Comments
0
Likes
0

Embeds 0

No embeds

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
    No notes for slide

Transcript

  • 1. Visual Basic 2005 III. STRINGS, CHARACTERS AND REGULAR EXPRESSIONS MELJUN CORTES
  • 2. In this chapter you will learn: •To create and manipulate immutable character String objects of class String. •To create and manipulate mutable character String objects of class StringBuilder. •To manipulate character objects of structure Char. •To use regular expressions in conjunction with classes Regex and Match. Strings, Characters and Regular Expressions Objectives STRINGS, CHARACTERS AND REGULAR EXPRESSIONS Visual Basic 2005 2
  • 3. Strings, Characters and Regular Expressions Characters are the fundamental building blocks of Visual Basic source code Every program is composed of characters that, when grouped together meaningfully, create a sequence that the compiler interprets as instructions a program also can contain character literals, also called character constants A character literal is a character that is represented internally as an integer value, called a character code Fundamentals of Characters and Strings STRINGS, CHARACTERS AND REGULAR EXPRESSIONS Visual Basic 2005 3
  • 4. Strings, Characters and Regular Expressions Character literals are established according to the Unicode character set A string is a series of characters treated as a single unit A string is an object of class String in the System namespace Fundamentals of Characters and Strings STRINGS, CHARACTERS AND REGULAR EXPRESSIONS Visual Basic 2005 4
  • 5. Strings, Characters and Regular Expressions We write string literals as sequences of characters in double quotation marks: “Gerick Andrei M. Salalima" “Gerick Andrei M. Salalima" “257-G Teresa St." “257-G Teresa St." “Sta. Mesa, Manila" “Sta. Mesa, Manila" "(632) 4689900" "(632) 4689900" A declaration can assign a String literal to a String variable: Dim color As String = “Blue” Dim color As String = “Blue” Fundamentals of Characters and Strings STRINGS, CHARACTERS AND REGULAR EXPRESSIONS Visual Basic 2005 5
  • 6. Strings, Characters and Regular Expressions Class String provides constructors for initializing Strings ' string initialization ' string initialization originalString = "Welcome to VB programming!" originalString = "Welcome to VB programming!" string1 = originalString string1 = originalString string2 = New String(characterArray) string2 = New String(characterArray) string3 = New String(characterArray, 6, 3) string3 = New String(characterArray, 6, 3) string4 = New String("C"c, 5) string4 = New String("C"c, 5) String Constructors STRINGS, CHARACTERS AND REGULAR EXPRESSIONS Visual Basic 2005 6
  • 7. Strings, Characters and Regular Expressions Sample program ‘Demonstrating String class constructors ‘Demonstrating String class constructors Module StringConstructor Module StringConstructor Sub Main() Sub Main() Dim originalString, string1, string2, _ Dim originalString, string1, string2, _ string3, string4 As String string3, string4 As String Dim characterArray() As Char = _ Dim characterArray() As Char = _ {"b"c, "i"c, "r"c, "t"c, "h"c, " "c, "d"c, "a"c, "y"c} {"b"c, "i"c, "r"c, "t"c, "h"c, " "c, "d"c, "a"c, "y"c} ' string initialization ' string initialization originalString = "Welcome to VB programming!" originalString = "Welcome to VB programming!" string1 = originalString string1 = originalString string2 = New String(characterArray) string2 = New String(characterArray) string3 = New String(characterArray, 6, 3) string3 = New String(characterArray, 6, 3) string4 = New String("C"c, 5) string4 = New String("C"c, 5) Console.WriteLine("string1 = " & """" & string1 & _ Console.WriteLine("string1 = " & """" & string1 & _ """" & vbCrLf & "string2 = " & """" & string2 & """" & vbCrLf & "string2 = " & """" & string2 & """" & vbCrLf & "string3 = " & """" & string3 & """" & vbCrLf & "string3 = " & """" & string3 & """" & vbCrLf & "string4 = " & """" & string4 & """" & vbCrLf & "string4 = " & """" & string4 & """" & vbCrLf) """" & vbCrLf) End Sub ' Main End Sub ' Main End Module ' StringConstructor End Module ' StringConstructor _ _ _ _ _ _ String Constructors STRINGS, CHARACTERS AND REGULAR EXPRESSIONS Visual Basic 2005 7
  • 8. Strings, Characters and Regular Expressions Output string1 string1 string2 string2 string3 string3 string4 string4 = = = = = = = = "Welcome to VB programming!" "Welcome to VB programming!" "birth day" "birth day" "day" "day" "CCCCC" "CCCCC" String Constructors STRINGS, CHARACTERS AND REGULAR EXPRESSIONS Visual Basic 2005 8
  • 9. Strings, Characters and Regular Expressions The String indexer is used to access individual characters in a String The property Length returns the length of the String The CopyTo method copies a specified number of characters from a String into a Char array String Indexer, Length Property and CopyTo Method STRINGS, CHARACTERS AND REGULAR EXPRESSIONS Visual Basic 2005 9
  • 10. Strings, Characters and Regular Expressions Using the indexer, property Length and method CopyTo Dim string1 As String = “Hello there” Dim string1 As String = “Hello there” Dim i As Integer Dim i As Integer For i = string1.Length - 1 To 0 Step -1 For i = string1.Length - 1 To 0 Step -1 Console.Write(string1(i)) Console.Write(string1(i)) Next i Next i Dim len As Integer Dim len As Integer len = string1.Length len = string1.Length Dim characterArray() As Char Dim characterArray() As Char ' copy characters from string1 into characterArray ' copy characters from string1 into characterArray string1.CopyTo(0, characterArray, 0, 5) string1.CopyTo(0, characterArray, 0, 5) String Indexer, Length Property and CopyTo Method STRINGS, CHARACTERS AND REGULAR EXPRESSIONS Visual Basic 2005 10
  • 11. Strings, Characters and Regular Expressions Comparing Strings with = and String Methods Equals and CompareTo ' test for equality with = ' test for equality with = If string1 = "hello” Then If string1 = "hello” Then Console.WriteLine("string1 equals ""hello""") Console.WriteLine("string1 equals ""hello""") End If End If ' test for equality using Equals method ' test for equality using Equals method If string1.Equals("hello") Then If string1.Equals("hello") Then Console.WriteLine("string1 equals ""hello""") Console.WriteLine("string1 equals ""hello""") End If End If ' test CompareTo ' test CompareTo If string1.CompareTo("hello") If string1.CompareTo("hello") Console.WriteLine("string1 Console.WriteLine("string1 Else Else Console.WriteLine("string1 Console.WriteLine("string1 End If End If = 0 Then = 0 Then equals ""hello""") equals ""hello""") not equal to ""hello""") not equal to ""hello""") Comparing Strings STRINGS, CHARACTERS AND REGULAR EXPRESSIONS Visual Basic 2005 11
  • 12. Strings, Characters and Regular Expressions In many applications, it is necessary to search for a character or set of characters in a String For example, a programmer creating a word processor would want to provide capabilities for searching through documents Locating Characters and Substrings in Strings STRINGS, CHARACTERS AND REGULAR EXPRESSIONS Visual Basic 2005 12
  • 13. Strings, Characters and Regular Expressions Searching for characters and substrings in Strings Dim Dim Dim Dim Dim Dim letters As String = "abcdefghijklmabcdefghijklm“ letters As String = "abcdefghijklmabcdefghijklm“ searchLetters As Char () = {"c"c, "a"c, "$"c} searchLetters As Char () = {"c"c, "a"c, "$"c} index As Integer index As Integer index index . . . . . . index index . . . . . . index index = letters.IndexOf("c"c) = letters.IndexOf("c"c) ‘this returns 2 ‘this returns 2 = letters.IndexOf("a"c, 1) = letters.IndexOf("a"c, 1) ‘this returns 13 ‘this returns 13 = letters.IndexOf("$"c, 3, 5) = letters.IndexOf("$"c, 3, 5) ‘this returns -1 ‘this returns -1 No of characters to seek IndexOf retruns the index of the first occurrence of the character in the string Starting index where search begins Character to be located Locating Characters and Substrings in Strings STRINGS, CHARACTERS AND REGULAR EXPRESSIONS Visual Basic 2005 13
  • 14. Strings, Characters and Regular Expressions Searching for characters and substrings in Strings Dim Dim Dim Dim Dim Dim letters As String = "abcdefghijklmabcdefghijklm“ letters As String = "abcdefghijklmabcdefghijklm“ searchLetters As Char () = {"c"c, "a"c, "$"c} searchLetters As Char () = {"c"c, "a"c, "$"c} index As Integer index As Integer index index . . . . . . index index . . . . . . index index = letters.LastIndexOf("c"c) = letters.LastIndexOf("c"c) ‘this returns 15 ‘this returns 15 = letters.LastIndexOf("a"c, 25) = letters.LastIndexOf("a"c, 25) ‘this returns 13 ‘this returns 13 = letters.LastIndexOf("$"c, 15, 5) = letters.LastIndexOf("$"c, 15, 5) ‘this returns -1 ‘this returns -1 No of characters to seek LastIndexOf retruns the index of the last occurrence of the character in the string Starting index where search begins Character to be located Locating Characters and Substrings in Strings STRINGS, CHARACTERS AND REGULAR EXPRESSIONS Visual Basic 2005 14
  • 15. Strings, Characters and Regular Expressions Searching for characters and substrings in Strings Dim Dim Dim Dim Dim Dim letters As String = "abcdefghijklmabcdefghijklm“ letters As String = "abcdefghijklmabcdefghijklm“ searchLetters As Char () = {"c"c, "a"c, "$"c} searchLetters As Char () = {"c"c, "a"c, "$"c} index As Integer index As Integer index index . . . . . . index index . . . . . . index index = letters.IndexOfAny(searchLetters) = letters.IndexOfAny(searchLetters) ‘this returns 0 ‘this returns 0 = letters.IndexOfAny(searchLetters, 7) = letters.IndexOfAny(searchLetters, 7) ‘this returns 13 ‘this returns 13 = letters.IndexOfAny(searchLetters, 7, 5) = letters.IndexOfAny(searchLetters, 7, 5) ‘this returns -1 ‘this returns -1 IndexOfAny retruns the index of the first occurrence in the string of any character in a specified array of unicode characters Locating Characters and Substrings in Strings STRINGS, CHARACTERS AND REGULAR EXPRESSIONS Visual Basic 2005 15
  • 16. Strings, Characters and Regular Expressions Searching for characters and substrings in Strings Dim Dim Dim Dim Dim Dim letters As String = "abcdefghijklmabcdefghijklm“ letters As String = "abcdefghijklmabcdefghijklm“ searchLetters As Char () = {"c"c, "a"c, "$"c} searchLetters As Char () = {"c"c, "a"c, "$"c} index As Integer index As Integer index index . . . . . . index index . . . . . . index index = letters.LastIndexOfAny(searchLetters) = letters.LastIndexOfAny(searchLetters) ‘this returns 15 ‘this returns 15 = letters.LastIndexOfAny(searchLetters, 1) = letters.LastIndexOfAny(searchLetters, 1) ‘this returns 0 ‘this returns 0 = letters.LastIndexOfAny(searchLetters, 25, 5)‘this returns -1 = letters.LastIndexOfAny(searchLetters, 25, 5)‘this returns -1 LastIndexOfAny retruns the index of the last occurrence in the string of any character in a specified array of unicode characters Locating Characters and Substrings in Strings STRINGS, CHARACTERS AND REGULAR EXPRESSIONS Visual Basic 2005 16
  • 17. Strings, Characters and Regular Expressions Class String provides two Substring methods, which are used to create a new String by copying part of an existing String Dim letters As String = "abcdefghijklmabcdefghijklm“ Dim letters As String = "abcdefghijklmabcdefghijklm“ Dim output As String = “” Dim output As String = “” output output . . . . . . output output . . . . . . = letters.Substring(20) = letters.Substring(20) ‘output gets hijklm ‘output gets hijklm = letters.Substring(0, 6) ‘output gets abcdef = letters.Substring(0, 6) ‘output gets abcdef Starting index No of characters to extract from starting index Extracting Substrings From Strings STRINGS, CHARACTERS AND REGULAR EXPRESSIONS Visual Basic 2005 17
  • 18. Strings, Characters and Regular Expressions Like the & operator, the String class's Shared method Concat can be used to concatenate two Strings Dim Dim Dim Dim Dim Dim string1 As String = “Visual “ string1 As String = “Visual “ string2 As String = “Basic” string2 As String = “Basic” newString As String newString As String newString = String.Concat(string1, string2) newString = String.Concat(string1, string2) ‘newString gets Visual Basic ‘newString gets Visual Basic . . . . . . Concat method Concatenating Strings STRINGS, CHARACTERS AND REGULAR EXPRESSIONS Visual Basic 2005 18
  • 19. Strings, Characters and Regular Expressions Class String provides several methods that return modified copies of Strings String methods Replace, ToLower, ToUpper and Trim: Dim Dim Dim Dim Dim Dim Dim Dim string1 As String string1 As String string2 As String string2 As String string3 As String string3 As String output As String output As String output output . . . . . . output output . . . . . . output output . . . . . . output output . . . . . . = = = = = = “cheers!“ “cheers!“ “GOOD BYE ” “GOOD BYE ” “ spaces “ “ spaces “ = string1.Replace(“e”c, “E”c) ‘output gets “chEErs!” = string1.Replace(“e”c, “E”c) ‘output gets “chEErs!” = string1.ToUpper() = string1.ToUpper() ‘output gets “CHEERS!” ‘output gets “CHEERS!” = string2.ToLower() = string2.ToLower() ‘output gets “good bye” ‘output gets “good bye” = string3.Trim() = string3.Trim() ‘output gets “spaces” ‘output gets “spaces” Miscellaneous String Methods STRINGS, CHARACTERS AND REGULAR EXPRESSIONS Visual Basic 2005 19
  • 20. Strings, Characters and Regular Expressions Objects of class String are constant strings, whereas object of class StringBuilder are mutable sequences of characters ' Demonstrating StringBuilder class constructors. ' Demonstrating StringBuilder class constructors. Imports System.Text Imports System.Text Module StringBuilderConstructor Module StringBuilderConstructor Sub Main() Sub Main() Dim buffer1, buffer2, buffer3 As StringBuilder Dim buffer1, buffer2, buffer3 As StringBuilder buffer1 = New StringBuilder() buffer1 = New StringBuilder() buffer2 = New StringBuilder(10) buffer2 = New StringBuilder(10) buffer3 = New StringBuilder("hello") buffer3 = New StringBuilder("hello") Console.WriteLine("buffer1 = """ & buffer1.ToString() Console.WriteLine("buffer1 = """ & buffer1.ToString() Console.WriteLine("buffer2 = """ & buffer2.ToString() Console.WriteLine("buffer2 = """ & buffer2.ToString() Console.WriteLine("buffer3 = """ & buffer3.ToString() Console.WriteLine("buffer3 = """ & buffer3.ToString() End Sub ' Main End Sub ' Main End Module ' StringBuilderConstructor End Module ' StringBuilderConstructor & & & & & & """") """") """") """") """") """") Class StringBuilder STRINGS, CHARACTERS AND REGULAR EXPRESSIONS Visual Basic 2005 20
  • 21. Strings, Characters and Regular Expressions Class StringBuilder provides the Length and Capacity properties to return the number of characters currently in a StringBuilder and the number of characters that a StringBuilder can store without allocating more memory, respectively Length and Capacity Properties, EnsureCapacity Method and Indexer of Class StringBuilder STRINGS, CHARACTERS AND REGULAR EXPRESSIONS Visual Basic 2005 21
  • 22. Strings, Characters and Regular Expressions StringBuilder size manipulation Dim buffer As New StringBuilder("Hello, how are you?") Dim buffer As New StringBuilder("Hello, how are you?") Console.WriteLine(buffer.ToString()) Console.WriteLine(buffer.ToString()) Console.WriteLine(“Length = “ & buffer.Length) Console.WriteLine(“Length = “ & buffer.Length) Console.WriteLine(“Capacity = “ & buffer.Capacity) Console.WriteLine(“Capacity = “ & buffer.Capacity) buffer.EnsureCapacity(75) buffer.EnsureCapacity(75) Buffer.Length = 10 Buffer.Length = 10 Console.WriteLine(buffer.ToString()) Console.WriteLine(buffer.ToString()) Hello, how are you? Hello, how are you? Length = 19 Length = 19 Capacity = 32 Capacity = 32 Hello, how Hello, how Length and Capacity Properties, EnsureCapacity Method and Indexer of Class StringBuilder STRINGS, CHARACTERS AND REGULAR EXPRESSIONS Visual Basic 2005 22
  • 23. Strings, Characters and Regular Expressions Class StringBuilder provides 19 overloaded Append methods for appending values of various types to the end of a StringBuilder's contents There are versions of this method for each of the primitive types and for character arrays, Strings and Objects Append and AppendFormat Methods of Class StringBuilder STRINGS, CHARACTERS AND REGULAR EXPRESSIONS Visual Basic 2005 23
  • 24. Strings, Characters and Regular Expressions Append Dim Dim Dim Dim Dim Dim Dim Dim Dim Dim Dim Dim Dim Dim Dim Dim Dim Dim Dim Dim methods of StringBuilder objectValue As Object = "hello“ objectValue As Object = "hello“ stringValue As String = "good bye“ stringValue As String = "good bye“ characterArray As Char() = {"a"c, "b"c, "c"c, "d"c, "e"c, "f"c} characterArray As Char() = {"a"c, "b"c, "c"c, "d"c, "e"c, "f"c} booleanValue As Boolean = True booleanValue As Boolean = True characterValue As Char = "Z"c characterValue As Char = "Z"c integerValue As Integer = 7 integerValue As Integer = 7 longValue As Long = 1000000 longValue As Long = 1000000 floatValue As Single = 2.5F ' F indicates that 2.5 is a float floatValue As Single = 2.5F ' F indicates that 2.5 is a float doubleValue As Double = 33.333 doubleValue As Double = 33.333 buffer As New StringBuilder() buffer As New StringBuilder() ' use method Append to append values to buffer ' use method Append to append values to buffer buffer.Append(objectValue) buffer.Append(objectValue) buffer.Append(" ") buffer.Append(" ") buffer.Append(stringValue) buffer.Append(stringValue) buffer.Append(" ") buffer.Append(" ") buffer.Append(characterArray) buffer.Append(characterArray) buffer.Append(" “) buffer.Append(" “) buffer.Append(integerValue) buffer.Append(integerValue) . . . . . . Append and AppendFormat Methods of Class StringBuilder STRINGS, CHARACTERS AND REGULAR EXPRESSIONS Visual Basic 2005 24
  • 25. Strings, Characters and Regular Expressions Class StringBuilder provides 18 overloaded Insert methods to allow values of various types to be inserted at any position in a StringBuilder There are versions of Insert for each of the primitive types and for character arrays, Strings and Objects Insert, Remove and Replace Methods of Class StringBuilder STRINGS, CHARACTERS AND REGULAR EXPRESSIONS Visual Basic 2005 25
  • 26. Strings, Characters and Regular Expressions StringBuilder text insertion and removal Dim buffer As New StringBuilder("Hello, how are you?") Dim buffer As New StringBuilder("Hello, how are you?") Console.WriteLine(buffer.ToString()) Console.WriteLine(buffer.ToString()) buffer.Insert(18, " Gerick") buffer.Insert(18, " Gerick") Console.WriteLine(buffer.ToString()) Console.WriteLine(buffer.ToString()) buffer.Replace(“Gerick", “Rainne") buffer.Replace(“Gerick", “Rainne") Console.WriteLine(buffer.ToString()) Console.WriteLine(buffer.ToString()) buffer.Remove(18, 7) buffer.Remove(18, 7) Console.WriteLine(buffer.ToString()) Console.WriteLine(buffer.ToString()) Hello, Hello, Hello, Hello, Hello, Hello, Hello, Hello, how how how how how how how how are are are are are are are are you? you? you Gerick? you Gerick? you Rainne? you Rainne? you? you? Insert, Remove and Replace Methods of Class StringBuilder STRINGS, CHARACTERS AND REGULAR EXPRESSIONS Visual Basic 2005 26
  • 27. Strings, Characters and Regular Expressions Many of the primitive types that we have used are actually aliases for different structures For instance, an Integer is defined by structure System.Int32, a Long by System.Int64 and so on Char, is the structure for characters Most Char methods are Shared Char Methods STRINGS, CHARACTERS AND REGULAR EXPRESSIONS Visual Basic 2005 27
  • 28. Strings, Characters and Regular Expressions Shared character-testing and caseconversion methods Char's Dim character As Char = “A”c Dim character As Char = “A”c Dim output As String Dim output As String output output output output output output output output output output output output output output output output output output = "is digit: " & Char.IsDigit(character) & vbCrLf = "is digit: " & Char.IsDigit(character) & vbCrLf &= "is letter: " & Char.IsLetter(character) & vbCrLf &= "is letter: " & Char.IsLetter(character) & vbCrLf &= "is letter or digit: " & _ &= "is letter or digit: " & _ Char.IsLetterOrDigit(character) & vbCrLf Char.IsLetterOrDigit(character) & vbCrLf &= "is lowercase: " & Char.IsLower(character) & vbCrLf &= "is lowercase: " & Char.IsLower(character) & vbCrLf &= "is uppercase: " & Char.IsUpper(character) & vbCrLf &= "is uppercase: " & Char.IsUpper(character) & vbCrLf &= "to uppercase: " & Char.ToUpper(character) & vbCrLf &= "to uppercase: " & Char.ToUpper(character) & vbCrLf &= "to lowercase: " & Char.ToLower(character) & vbCrLf &= "to lowercase: " & Char.ToLower(character) & vbCrLf &= "is punctuation: " & _ &= "is punctuation: " & _ Char.IsPunctuation(character) & vbCrLf Char.IsPunctuation(character) & vbCrLf &= "is symbol: " & Char.IsSymbol(character) &= "is symbol: " & Char.IsSymbol(character) Console.WriteLine(output) Console.WriteLine(output) Char Methods STRINGS, CHARACTERS AND REGULAR EXPRESSIONS Visual Basic 2005 28
  • 29. Strings, Characters and Regular Expressions Shared character-testing and caseconversion methods Char's is is is is is is is is is is to to to to is is is is digit: False digit: False letter: True letter: True letter or digit: True letter or digit: True lowercase: False lowercase: False uppercase: True uppercase: True uppercase: A uppercase: A lowercase: a lowercase: a punctuation: False punctuation: False symbol: False symbol: False Char Methods STRINGS, CHARACTERS AND REGULAR EXPRESSIONS Visual Basic 2005 29
  • 30. Strings, Characters and Regular Expressions Regular expressions are specially formatted Strings used to find (and possibly replace) patterns in text They can be useful during information validation, to ensure that data is in a particular format For example, a Philippine ZIP code must consist of four digits, and a last name must start with a capital letter Regular Expressions and Class RegEx STRINGS, CHARACTERS AND REGULAR EXPRESSIONS Visual Basic 2005 30
  • 31. Strings, Characters and Regular Expressions Regular expression character classes: Character class Matches Character class Matches d Any digit D Any non-digit w Any word character W Any non-word character s Any whitespace S Any non-whitespace Regular Expressions and Class RegEx STRINGS, CHARACTERS AND REGULAR EXPRESSIONS Visual Basic 2005 31
  • 32. Strings, Characters and Regular Expressions Regular expressions checking birthdays ‘ Demonstrating Class Regex. ‘ Demonstrating Class Regex. Imports System.Text.RegularExpressions Imports System.Text.RegularExpressions Module RegexMatches Module RegexMatches Sub Main() Sub Main() ' create regular expression ' create regular expression Dim expression As New Regex("J.*d[0-35-9]-dd-dd") Dim expression As New Regex("J.*d[0-35-9]-dd-dd") Dim string1 As String = _ Dim string1 As String = _ "Jane's Birthday is 05-12-75" & vbCrLf & _ "Jane's Birthday is 05-12-75" & vbCrLf & _ "Dave's Birthday is 11-04-68" & vbCrLf & _ "Dave's Birthday is 11-04-68" & vbCrLf & _ "John's Birthday is 04-28-73" & vbCrLf & _ "John's Birthday is 04-28-73" & vbCrLf & _ "Joe's Birthday is 12-17-77" "Joe's Birthday is 12-17-77" ' match regular expression to string and ' match regular expression to string and ' print out all matches ' print out all matches For Each myMatch As Match In expression.Matches(string1) For Each myMatch As Match In expression.Matches(string1) Console.WriteLine(myMatch) Console.WriteLine(myMatch) Next myMatch Next myMatch End Sub ' Main End Sub ' Main End Module ' RegexMatches End Module ' RegexMatches Regular Expressions and Class RegEx STRINGS, CHARACTERS AND REGULAR EXPRESSIONS Visual Basic 2005 32
  • 33. Strings, Characters and Regular Expressions Quantifiers Quantifier used in regular expressions Matches * Matches zero or more occurrences of the preceding pattern. + Matches one or more occurrences of the preceding pattern. ? Matches zero or one occurrences of the preceding pattern. {n} Matches exactly n occurrences of the preceding pattern. {n,} Matches at least n occurrences of the preceding pattern. {n,m} Matches between n and m (inclusive) occurrences of the preceding pattern. Regular Expressions and Class RegEx STRINGS, CHARACTERS AND REGULAR EXPRESSIONS Visual Basic 2005 33
  • 34. Strings, Characters and Regular Expressions Validating user information using regular expressions Dim strLastName As String = "Salalima" Dim strLastName As String = "Salalima" If Regex.Match(strLastName, "^[A-Z][a-zA-Z]*$").Success Then If Regex.Match(strLastName, "^[A-Z][a-zA-Z]*$").Success Then MessageBox.Show("Valid last name", "Message", _ MessageBox.Show("Valid last name", "Message", _ MessageBoxButtons.OK, MessageBoxIcon.Information) MessageBoxButtons.OK, MessageBoxIcon.Information) Else Else MessageBox.Show("Invalid last name", "Message", _ MessageBox.Show("Invalid last name", "Message", _ MessageBoxButtons.OK, MessageBoxIcon.Error) MessageBoxButtons.OK, MessageBoxIcon.Error) End If End If Regular Expressions and Class RegEx STRINGS, CHARACTERS AND REGULAR EXPRESSIONS Visual Basic 2005 34
  • 35. Strings, Characters and Regular Expressions Validating user information using regular expressions Dim strAddress As String = "257-G Teresa St." Dim strAddress As String = "257-G Teresa St." If Regex.Match(strAddress, _ If Regex.Match(strAddress, _ "^[0-9]+s+([a-zA-Z]+|[a-zA-Z]+s[a-zA-Z]+)$").Success Then "^[0-9]+s+([a-zA-Z]+|[a-zA-Z]+s[a-zA-Z]+)$").Success Then MessageBox.Show("Valid address", "Message", _ MessageBox.Show("Valid address", "Message", _ MessageBoxButtons.OK, MessageBoxIcon.Information) MessageBoxButtons.OK, MessageBoxIcon.Information) Else Else MessageBox.Show("Invalid address", "Message", _ MessageBox.Show("Invalid address", "Message", _ MessageBoxButtons.OK, MessageBoxIcon.Error) MessageBoxButtons.OK, MessageBoxIcon.Error) End If End If Regular Expressions and Class RegEx STRINGS, CHARACTERS AND REGULAR EXPRESSIONS Visual Basic 2005 35
  • 36. Strings, Characters and Regular Expressions Validating user information using regular expressions Dim strZip As String = "12345" Dim strZip As String = "12345" If Regex.Match(strZip, "^d{5}$").Success Then If Regex.Match(strZip, "^d{5}$").Success Then MessageBox.Show("Valid zip code", "Message", _ MessageBox.Show("Valid zip code", "Message", _ MessageBoxButtons.OK, MessageBoxIcon.Information) MessageBoxButtons.OK, MessageBoxIcon.Information) Else Else MessageBox.Show("Invalid zip code", "Message", _ MessageBox.Show("Invalid zip code", "Message", _ MessageBoxButtons.OK, MessageBoxIcon.Error) MessageBoxButtons.OK, MessageBoxIcon.Error) End If End If Regular Expressions and Class RegEx STRINGS, CHARACTERS AND REGULAR EXPRESSIONS Visual Basic 2005 36
  • 37. Exercise STRINGS, CHARACTERS AND REGULAR EXPRESSIONS Visual Basic 2005 37
  • 38. End of Strings, Characters and Regular Expressions STRINGS, CHARACTERS AND REGULAR EXPRESSIONS Visual Basic 2005 38