2011-12-13 53 views
-2

我有一个RegEx我在vb.net中使用,我可以获得文本的所有匹配,但我想要获得文本的第一个匹配项,这是我正在使用的。如何使用vb.net中的正则表达式从文本中获得第一个匹配

文字::1283 xxxxxxxxxx :1288 :9879

正则表达式::[0-9]+

如何我刚刚得到的第一场比赛(:1283)?

我使用的代码是:

Dim MRIDRegex As New Regex(":[0-9]+") 
    Dim count As Integer = 0 
    Dim mrid As String = String.Empty 
    For Each item As Match In MRIDRegex.Matches(message) 

     count += 1 
     If count = 1 Then 
      mrid = item.Value 
     End If 

    Next 
+2

你能提供你试过的代码吗? – FakeRainBrigand

+0

@FakeRainBrigand即时通讯使用一个在线vb.net正则表达式测试人 – redoc01

+0

人们需要解释他们为什么拒绝投票这个问题??????? – redoc01

回答

2

我搜索在谷歌vb.net regex match,这是the first result。这是他们使用的代码示例。

Imports System.Text.RegularExpressions 

Module Module1 

    Sub Main() 
    ' The input string. 
    Dim value As String = "/content/alternate-1.aspx" 

    ' Invoke the Match method. 
    Dim m As Match = Regex.Match(value, _ 
        "content/([A-Za-z0-9\-]+)\.aspx$", _ 
        RegexOptions.IgnoreCase) 

    ' If successful, write the group. 
    If (m.Success) Then 
     Dim key As String = m.Groups(1).Value 
     Console.WriteLine(key) 
    End If 
    End Sub 

End Module 
相关问题