I have a substring that may or may not exist in a larger string, and if it does it should contain some contact information that I want to grab with a regular expression.
Due to message parameters beyond my control, this substring may sometimes be truncated, so I’ve written a couple different regular expressions to accommodate for each scenario.
The problem I’m running into is that the more complicated expressions die on me. These expressions have run just fine on every Regex testing website that I’ve tried.
Here’s a code clip for reference.
' 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
Unfortunately Google has failed me (or I’ve failed it). Does anyone have any thoughts? Again, the regular expressions themselves evaluate successfully on Regex test sites.
You have probably encountered catastrophic backtracking. Your regexes contain nested repetitions of patterns that are not mutually exclusive. Especially
(\w*|\W*|\s*|\S*)*does not make any sense.\wand\Win combination contain all characters. So do\sand\S. Also the inner asterisks do not accomplish anything, because the outer repetition can take care of that as well.If, what you want to accomplish, really is to match any character there, you can simply replace every
(\w*|\W*|\s*|\S*)*by[\s\S]*. Alternatively.*in combination withRegexOptions.Singlelinedoes the same.