600

1200

1800

2400

3000

3600

4200

4800

5400

New Regex; regex instance .IsMatch
    Private Sub Rgx1()
        Dim re As New Regex("2.7")
        ' matches 247 
        If re.IsMatch("239-247-299-237-337") Then
            Console.WriteLine("Regex found")
        Else
            Console.WriteLine("Regex  not found")
        End If
    End Sub
   

Notice that the regular expressiion string is input to the New constructor. Thus the regular expression is part of the instance re.

Regex.IsMatch with Start index

  Private Sub Rgx2()
        Dim r As New Regex("2.7")
        Console.WriteLine("Start index = 8")
        ' matches 237 since start index is beyond 247
        If r.IsMatch("239-247-299-237-337", 8) Then
            Console.WriteLine("Regex found") ' found when start index is 8
        Else
            Console.WriteLine("Regex  not found")
        End If
        ' no matches found since start index is beyond all matches
        Console.WriteLine("Start index = 13")
        If r.IsMatch("239-247-299-237-337", 13) Then
            Console.WriteLine("Regex found")
        Else
            Console.WriteLine("Regex  not found") ' no found when start index is 13
        End If
    End Sub
  

Just like the previous except that the search does not start until the start index, thus jumping over potential matches

Regex.IsMatch(search string, regular expression string)
    Private Sub Rgx3()
        ' Regex.Ismatch(string to search,regular expression)
        If Regex.IsMatch("239-247-299-231-337", "2.7") Then
            Console.WriteLine("Regular expression found")
        Else
            Console.WriteLine("Regular expression NOT found")
        End If
    End Sub
  

Notice that I did not have to "Dim" an instance of "Regex" in this case. Both the search string and the regular expression string are input as in Regex.IsMatch instead of re.IsMatch

IsMatch(input string,pattern string,RegexOptions)
 Private Sub Rgx4()
      ' this .IsMatch has only 2 parameters
        If Regex.IsMatch("aBc Def ghI", "def") Then 
            Console.WriteLine("Regular expression found")
        Else
            Console.WriteLine("Regular expression NOT found")
        End If
        ' this following IsMatch adds a third parameter: RegexOptions.IgnoreCase
        If Regex.IsMatch("aBc Def ghI", "def", RegexOptions.IgnoreCase) Then
            Console.WriteLine("Regular expression found")
        Else
            Console.WriteLine("Regular expression NOT found")
        End If
    End Sub
  

The routine uses the RegexOptions.IgnoreCase option. So "def" matched on "Def". Ignoring case can be done with the pattern string alone, but this way is handy if you don't want to use a more complicated regular expression.


11/11/2015
TheLonelyProgrammer.Net
Alone in a sea of non-programmers
Home Random Regular expressions Create control at runtime Text files: Read & Write