2013-12-20 34 views
1

如何獲取名稱?Visual Basic 2013正則表達式

「你好名稱!」

Dim r As New Regex("Hello (.*)! :)") 
      Dim matches As MatchCollection = r.Matches(Chat) 
      For Each m As Match In matches 
       MsgBox("Hi " & m.ToString & " and welcome back!") 
      Next 

(聊天是最後Chatmessage)

回答

3

要獲得唯一的名字,你應該使用Groups。如果你真的想匹配:),你必須使用\逃脫)

Dim Chat As String = "Hello Name!" 
Dim r As New Regex("Hello (.*)! :\)") 
Dim matches As MatchCollection = r.Matches(Chat) 
For Each m As Match In matches 
    m.Groups(1).Value 
Next 
1

你爲什麼不這樣做一個字符串替換爲你好你知道這些是固定的?

我不知道VB,但其他語言的REGEX應該是Hello(\ S +)!