VB.Net Tutorial/Regular Expressions/Regular Expressions

Материал из VB Эксперт
Перейти к: навигация, поиск

Count chars: Regex.Matches(quote, .)

<source lang="vbnet">Imports System.Text.RegularExpressions Public Class Tester

   Public Shared Sub Main
       Dim quote As String = _
           "The important thing" & vbNewLine & _
           "is not to stop questioning." & vbNewLine & _
           "--Albert Einstein" & vbNewLine
       Dim numChars As Integer = Regex.Matches(quote, ".").Count
       Console.WriteLine(numChars)
   End Sub

End Class</source>

66

Count line: Regex.Matches(quote, ".+\n*")

<source lang="vbnet">Imports System.Text.RegularExpressions Public Class Tester

   Public Shared Sub Main
       Dim quote As String = _
           "The important thing" & vbNewLine & _
           "is not to stop questioning." & vbNewLine & _
           "--Albert Einstein" & vbNewLine
       Dim numLines As Integer = Regex.Matches(quote, ".+\n*").Count
       Console.WriteLine(numLines)
   End Sub

End Class</source>

3

Every word replaced by another word

<source lang="vbnet">Imports System.Text.RegularExpressions Public Class Tester

   Public Shared Sub Main
   
      Console.WriteLine(Regex.Replace("This sentence ends in 5 stars *****", "\w+", "word"))
   End Sub

End Class</source>

word word word word word word *****

Match regular expression to string and print out all matches

<source lang="vbnet">Imports System.Text.RegularExpressions Module Tester

  Sub Main()
     Dim myMatch As Match
     Dim expression As Regex = New Regex("J.*\d[0-35-9]-\d\d-\d\d")
     Dim string1 As String = "11-11-75" & _
        vbCrLf & "is 11-05-68" & vbCrLf & _
        "asdf 04-18-73" & vbCrLf & _
        "fdsa 12-27-77"
     
     For Each myMatch In expression.Matches(string1)
        Console.WriteLine(myMatch.ToString())
     Next
  End Sub 

End Module</source>

Regex.CompileToAssembly

<source lang="vbnet">Imports System.Text.RegularExpressions Public Class Tester

   Public Shared Sub Main
       Dim numPattern As String = _
          "[-+]?([0-9]*\.)?[0-9]+([eE][-+]?[0-9]+)?"
       Dim wordPattern As String = "\w+"
       Dim whichNamespace As String = "NumbersRegex"
       Dim isPublic As Boolean = True
       Dim compNumbers As New RegexCompilationInfo(numPattern, _
          RegexOptions.rupiled, "RgxNumbers", _
          whichNamespace, isPublic)
       Dim compWords As New RegexCompilationInfo(wordPattern, _
          RegexOptions.rupiled, "RgxWords", whichNamespace, _
          isPublic)
       Dim compAll() As RegexCompilationInfo = _
          {compNumbers, compWords}
       Dim whichAssembly As New _
          System.Reflection.AssemblyName("RgxNumbersWords")
       Regex.rupileToAssembly(compAll, whichAssembly)
   End Sub

End Class</source>

Regex Matches

<source lang="vbnet">Imports System.Text.RegularExpressions Public Class Tester

   Public Shared Sub Main
       Dim quote As String = "AA BB in"
       Dim count1 As Integer
       Dim count2 As Integer
       Dim count3 As Integer
       count1 = Regex.Matches(quote, "(in)+").Count
       count2 = Split(quote, "in").Length - 1
       Dim content As String = "in"
       Dim position As Integer = -content.Length
       Do
           position = quote.IndexOf(content, position + content.Length)
           If (position < 0) Then Exit Do
           count3 += 1
       Loop
       Console.WriteLine(String.Format( _
           "{0}{3}{1}{3}{2}", count1, count2, count3, vbNewLine))    
   
   End Sub

End Class</source>

1
1
1

Regex("\w+")

<source lang="vbnet">Imports System.Text.RegularExpressions Public Class Tester

   Public Shared Sub Main
       Dim quote As String = "The important thing is not to " & _
          "stop questioning. --Albert Einstein"
       Dim parser As New Regex("\w+")
       Dim totalMatches As Integer = parser.Matches(quote).Count
       Console.WriteLine(quote & vbNewLine & "Number words: " & _
          totalMatches.ToString)
   End Sub

End Class</source>

The important thing is not to stop questioning. --Albert Einstein
Number words: 10

Replace first 3 digits

<source lang="vbnet">Imports System.Text.RegularExpressions Public Class Tester

   Public Shared Sub Main
   
      Console.WriteLine(New Regex("\d").Replace("1, 2, 3, 4, 5, 6, 7, 8", "digit", 3))
   End Sub

End Class</source>

digit, digit, digit, 4, 5, 6, 7, 8

String split at commas

<source lang="vbnet">Imports System.Text.RegularExpressions Public Class Tester

   Public Shared Sub Main
   
      Console.WriteLine(Regex.Split("1, 2, 3, 4, 5, 6, 7, 8", ",\s*"))
   End Sub

End Class</source>

System.String[]

Using Regex method Replace: Replace one string with another

<source lang="vbnet">Imports System.Text.RegularExpressions Public Class Tester

   Public Shared Sub Main
   
      Console.WriteLine(New Regex("stars").Replace("This sentence ends in 5 stars *****", "carets

))

   End Sub

End Class</source>

This sentence ends in 5 carets *****

Using Regex method Replace: substituted for *

<source lang="vbnet">Imports System.Text.RegularExpressions Public Class Tester

   Public Shared Sub Main
      Console.WriteLine(Regex.Replace("This sentence ends in 5 stars *****", "\*", ""))
   End Sub

End Class</source>

This sentence ends in 5 stars ^^^^^

Word count

<source lang="vbnet">Imports System.Text.RegularExpressions Public Class Tester

   Public Shared Sub Main
       Dim quote As String = "q d e w "
       Do While (quote.IndexOf(Space(2)) >= 0)
           quote = quote.Replace(Space(2), Space(1))
       Loop
       Dim wordCount As Integer = Split(quote, Space(1)).Length
       Console.WriteLine(quote & vbNewLine & "Number of words: " & wordCount.ToString)   
   
   End Sub

End Class</source>

q d e w
Number of words: 5

Word count: Regex.Matches(quote, "\w+")

<source lang="vbnet">Imports System.Text.RegularExpressions Public Class Tester

   Public Shared Sub Main
       Dim quote As String = _
           "The important thing" & vbNewLine & _
           "is not to stop questioning." & vbNewLine & _
           "--Albert Einstein" & vbNewLine
       Dim numWords As Integer = Regex.Matches(quote, "\w+").Count
       Console.WriteLine(numWords)
   End Sub

End Class</source>

10