I have a problem processing a regex in VB. My Text is:
is sadly currently in Toronto main station undetermined late
I tested my regex in Expresso and found this regex for my purpose. The thing is: Toronto main station can also be only “Toronto”. So this is my pattern:
is sadly currently in (([A-Za-z]*)(\s|-)){1,3}(.|\s)*?undetermined
The problem is processing the regex in VB, because my Pattern gives me a result like this:
- is sadly currently in Toronto main station undetermined
- undetermined
- Toronto
- Main
- undetermined
- undetermined
- Toronto
- Main
- undetermined
….
- undetermined
But I cannot access the words toronto and main via VB – also I dont want ‘undetermined’ be part of the result. I tried match.item(0).submatches.item(0).submatches.item(0) but VBA already throws an error if I try match.item(0).submatches.item(0).submatches and states that there is no such object – obviously it cannot process some those “multilevel” regexes. Is there a way to improve my pattern so that I only have to use one submatches or is it possible to use multiple submatches via VBA?!
Edit:
GetDelay.Pattern = is sadly currently in (([A-Za-z]*)(\s|-)){1,3}(.|\s)*?undetermined"
GetDelay.IgnoreCase = True
GetDelay.Multiline = True
...
If GetDelay.TEst(MailBody) Then
Set m = GetDelay.Execute(MailBody)
If m.Item(0).SubMatches.Count > 0 Then
OrtBody = m.Item(0).SubMatches.Item(0).SubMatches.Item(0) + " " + m.Item(0).SubMatches.Item(0).SubMatches.Item(1) 'Error 424 comes here - Object required
If GetReason.TEst(AbweichungsmailBody) Then
Set m = GetReason.Execute(AbweichungsmailBody)
If m.Item(0).SubMatches.Count > 0 Then
Reason= m.Item(0).SubMatches.Item(0)
Else
Reason= "Error!"
End If
Else
Reason = "Keine Angabe gefunden!"
End If
Else
thisfunction= False
End If
Else
thisfunction= False
End If
Capturing results from a repeated sub-expression (in your case
(([A-Za-z]*)(\s|-)){1,3}) gets tricky in any language. I suggest the following as a simpler approach:1) Match the whole portion you are interested in with a simple Regex:
2) Once you have matched the portion of interest, perform further analysis to get what you want. You could use another RegEx for this step, though I think you could just
Split()with a space as the delimiter.