I thought I was creating backreferances in this string
" System Serial Number: 1036358 (oldwlrfsr200" with RegEx.Pattern = "(System
Serial Number):(.*)\s(\(.*\))"
I expected three MATCH items but the match was the whole string. I thought
this would have created three matches, what happened? If I can create three
matches from this string, how can I referance a match directly?

Re: regex backreferance question by ekkehard

ekkehard
Fri Aug 31 11:53:31 PDT 2007

scott schrieb:
> I thought I was creating backreferances in this string
> " System Serial Number: 1036358 (oldwlrfsr200" with RegEx.Pattern = "(System
> Serial Number):(.*)\s(\(.*\))"
> I expected three MATCH items but the match was the whole string. I thought
> this would have created three matches, what happened? If I can create three
> matches from this string, how can I referance a match directly?
>
>
I *think* you should change your pattern a bit and use the Submatches
of the Match to get at your 3 parts:

Dim aTests : aTests = Array( _
"dontcare System Serial Number: 1036358 (oldwlrfsr200)dontcare" _
, "dontcare System Serial Number: AAA6358 (abracdabra12)dontcare" _
)
Dim oRE : Set oRE = New Regexp
oRE.Pattern = "(System Serial Number):\s([^ ]+)\s+(\([^)]+\))"
Dim sTest, oMTS, oMT
For Each sTest In aTests
Set oMTS = oRE.Execute( sTest )
If 1 = oMTS.Count Then
Set oMT = oMTS( 0 ) ' 1 match, but 3 captures/submatches
WScript.Echo oMT.SubMatches( 0 ) _
, oMT.SubMatches( 1 ) _
, oMT.SubMatches( 2 )
End If
Next

output:

=== reSerNo: match serial number ============
System Serial Number 1036358 (oldwlrfsr200)
System Serial Number AAA6358 (abracdabra12)
=== reSerNo: 0 done (00:00:00) ==============

Re: regex backreferance question by scott

scott
Fri Aug 31 12:38:58 PDT 2007

Thanks, I completly missed the submatches collection. This helped a lot!

"ekkehard.horner" <ekkehard.horner@arcor.de> wrote in message
news:46d863aa$0$16117$9b4e6d93@newsspool1.arcor-online.net...
> scott schrieb:
>> I thought I was creating backreferances in this string
>> " System Serial Number: 1036358 (oldwlrfsr200" with RegEx.Pattern =
>> "(System Serial Number):(.*)\s(\(.*\))"
>> I expected three MATCH items but the match was the whole string. I
>> thought this would have created three matches, what happened? If I can
>> create three matches from this string, how can I referance a match
>> directly?
> I *think* you should change your pattern a bit and use the Submatches
> of the Match to get at your 3 parts:
>
> Dim aTests : aTests = Array( _
> "dontcare System Serial Number: 1036358 (oldwlrfsr200)dontcare" _
> , "dontcare System Serial Number: AAA6358 (abracdabra12)dontcare" _
> )
> Dim oRE : Set oRE = New Regexp
> oRE.Pattern = "(System Serial Number):\s([^ ]+)\s+(\([^)]+\))"
> Dim sTest, oMTS, oMT
> For Each sTest In aTests
> Set oMTS = oRE.Execute( sTest )
> If 1 = oMTS.Count Then
> Set oMT = oMTS( 0 ) ' 1 match, but 3 captures/submatches
> WScript.Echo oMT.SubMatches( 0 ) _
> , oMT.SubMatches( 1 ) _
> , oMT.SubMatches( 2 )
> End If
> Next
>
> output:
>
> === reSerNo: match serial number ============
> System Serial Number 1036358 (oldwlrfsr200)
> System Serial Number AAA6358 (abracdabra12)
> === reSerNo: 0 done (00:00:00) ==============