2010-05-12 39 views
1

好吧之間的文本,所以我想抓住淨正則表達式來獲得方括號標記

[用戶名]和[/用戶名]

我知道如何讓之間的一個網站,這就是信息字符串,但我怎麼會使用正則表達式只有在中間的信息。

請記住,我將在頁面上有更多的東西。

+0

我不能告訴你是否不知道這一點,所以我會問:你知道,VB.NET沒有正則表達式?它是具有正則表達式的.NET Framework。 – 2010-05-12 20:02:46

+0

@John:我們仍然需要知道OP使用哪種語言來製作最有用的答案; ** VB.NET正則表達式**是一種很好,簡潔的方式來提供這兩種信息。 – 2010-05-12 21:15:20

+0

我正在使用VB.net。 – xZerox 2010-05-12 22:17:03

回答

3
'Sample input 
    Dim html = "<html><head><title>Test</title></head>" & vbNewLine & "<body><p>[usernames]Your Name Here[/usernames]</p>[usernames]Another Name Here[/usernames]</body></html>" 
    'Named pattern 
    Dim p = "\[usernames\](?<UserNames>.*?)\[/usernames\]" 
    'Grab all of the matches 
    Dim Matches = Regex.Matches(html, p, RegexOptions.IgnoreCase Or RegexOptions.Singleline) 
    'Make sure we found something 
    If Matches IsNot Nothing AndAlso Matches.Count > 0 Then 
     'Loop through all of the matches 
     For Each Match As Match In Matches 
      'Make sure our sub-group was a success 
      If Match.Groups("UserNames").Success Then 
       Trace.WriteLine(Match.Groups("UserNames").Value) 
      End If 
     Next 
    End If 
1

您最好爲每個實例解析文檔。

正則表達式匹配一個實例是

/<usernames>([^<]+?)<\/usernames>/