VB.Net Tutorial/Regular Expressions/Match Number

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

Match and extract numbers

Imports System.Text.RegularExpressions
Public Class Tester
    Public Shared Sub Main
        Dim source As String = _
           "This 321.0 string -0.020 contains " & _
           "3.0E-17 several 1 2. 34 numbers"
        Dim parser As New _
           Regex("[-+]?([0-9]*\.)?[0-9]+([eE][-+]?[0-9]+)?")
        Dim sourceMatches As MatchCollection = parser.Matches(source)
        Dim counter As Integer
        Console.WriteLine(sourceMatches.Count.ToString() & vbNewLine)
        For counter = 0 To sourceMatches.Count - 1
            Console.WriteLine(sourceMatches(counter).Value.ToString())
            Console.WriteLine(CDbl(sourceMatches(counter).Value).ToString())
        Next counter

    End Sub

End Class
6
321.0
321
-0.020
-0.02
3.0E-17
3E-17
1
1
2
2
34
34