我有一个子字符串,它可能存在也可能不存在于较大的字符串中,如果存在,它应该包含一些我想用正则表达式获取的联系信息。
由于我无法控制消息参数,这个子字符串有时可能会被截断,所以我编写了几个不同的正则表达式来适应每种情况。
我遇到的问题是更复杂的表达式在我身上消失了。这些表达式在我尝试过的每个正则表达式测试网站上都运行良好。
这是一个供参考的代码片段。
' Look for contact information using regular expressions. Data we're looking for is in the format below
' "-- Contact: [name] [email] [phone]"
Dim ContactPattern As String
Dim ContactMatch As Match
If SomeStuff Then
' Only look for the [name] block
ContactPattern = "((?:-- Contact: \[)((\w*|\W*|\s*|\S*)*)\])"
' This match attempt works fine.
ContactMatch = Regex.Match(FullString, ContactPattern, RegexOptions.None)
' Do stuff with the results
ElseIf SomeOtherStuff Then
' Look for [name] and [email]
ContactPattern = "((?:-- Contact: \[)((\w*|\W*|\s*|\S*)*)\] \[((\w*|\W*|\s*|\S*)*)\])"
' This match attempt does not get processed. I receive the message below in the output window.
'The thread '<No Name>' (0x1f58) has exited with code 0 (0x0).
ContactMatch = Regex.Match(FullString, ContactPattern, RegexOptions.IgnoreCase)
' Do stuff with the results
ElseIf SomeOtherOtherStuff Then
' Look for [name] [email] and [phone]
ContactPattern = "((?:-- Contact: \[)((\w*|\W*|\s*|\S*)*)\] \[((\w*|\W*|\s*|\S*)*)\] \[((\w*|\W*|\s*|\S*)*)\])"
' This match attempt does not get processed. I receive the message below in the output window.
' "The thread '<No Name>' (0x1f58) has exited with code 0 (0x0)."
ContactMatch = Regex.Match(FullString, ContactPattern, RegexOptions.None)
' Do stuff with the results
End If
不幸的是,谷歌让我失望了(或者我失败了)。有人有想法吗?同样,正则表达式本身在 Regex 测试站点上成功评估。