提取分组数字及其在字符串中的位置

时间:2017-09-08 16:45:25

标签: vb.net

寻找循环字符串的方法,搜索分组的数字,将它们的位置存储在字符串中,它们的长度和值。

例如:

Private s as String = "Tes12tin5g_44_for156another4624."

Private numbers As New List(Of GroupedNumbers)

GroupNumbers(s)

变量数字应该包含下一个项目的列表:

Value 12, Position 3, Length 2
Value 5, Position 8, Lenght 1
Value 44, Position 11, Length 2
Value 156, Position 16, Lenght 3
Value 4624, Position 24, Lenght 4

假设位置从0开始。

我只能检测数字,但不知道如何在群组中检测它们。

Private Sub GroupNumbers(ByVal s As String)
    Dim pos As Integer = 0
    For Each c As Char In sTextoOrígen
        If IsNumeric(c) Then
            numbers.Add(New GroupedNumbers(AscW(c), pos, 1))
        End If
        pos += 1
    Next
End Sub

1 个答案:

答案 0 :(得分:1)

试试Regex:

  List<string> contacts = new List<string>();
    contacts.Add("message");
    Console.WriteLine(">>> REFRESH CONTACTS...");

for (int n = 0; n < contacts.Count; n++)
{
    Console.WriteLine("Step 1");
    if (!listBox1.Items.Contains(contacts[n]))
    {
        Console.WriteLine("Step 2");
        listBox1.Items.Insert(n, contacts[n]);
        Console.WriteLine("Step 3");
    }
    Console.WriteLine("Step 4");
}

您需要: Dim s As String = "Tes12tin5g_44_for156another4624." Dim r As New Regex("\d+") For Each m As Match In r.Matches(s) Debug.Print(String.Format("Value {0}, Position {1}, Length {2}", m.Value, m.Index, m.Length)) Next

这给了我:

Imports System.Text.RegularExpressions