Problem with RegEx and matches

  • Thread starter Richard L Rosenheim
  • Start date
R

Richard L Rosenheim

I'm trying to extract a numeric substring from a string. Here's the code
that I have:

Const patternNetTerms = "^.*?(\d+).*?$"
Dim regEx
Dim matches
Dim match

Set regEx = New RegExp
'Set regEx = CreateObject("vbscript.regexp")
regEx.Pattern = patternNetTerms
regEx.IgnoreCase = False
regEx.Global = True
regEx.MultiLine = False
Set matches = regEx.Execute(sNetTerms)

If matches.Count >= 1 Then
MsgBox "# of matches: " & matches.Count
For Each match In matches
MsgBox "Match: " & match.value
Next
End If

When the string is "Net 10 Days", the only output I'm getting is the entire
string. How do I retrieve the "10"? According to the regex calculator at
http://www.codehouse.com/webmaster_tools/regex/, my expression does works.
So, my problem appears to be in how I'm trying to retrieve the matches.

Anyone have any suggestions as to what I'm doing wrong?

TIA,

Richard
 
R

Richard L Rosenheim

Dana,

Thanks for the reply -- the match.SubMatches(0) solved my issue.

Richard
 
R

Ron Rosenfeld

I'm trying to extract a numeric substring from a string. Here's the code
that I have:

Const patternNetTerms = "^.*?(\d+).*?$"
Dim regEx
Dim matches
Dim match

Set regEx = New RegExp
'Set regEx = CreateObject("vbscript.regexp")
regEx.Pattern = patternNetTerms
regEx.IgnoreCase = False
regEx.Global = True
regEx.MultiLine = False
Set matches = regEx.Execute(sNetTerms)

If matches.Count >= 1 Then
MsgBox "# of matches: " & matches.Count
For Each match In matches
MsgBox "Match: " & match.value
Next
End If

When the string is "Net 10 Days", the only output I'm getting is the entire
string. How do I retrieve the "10"? According to the regex calculator at
http://www.codehouse.com/webmaster_tools/regex/, my expression does works.
So, my problem appears to be in how I'm trying to retrieve the matches.

Anyone have any suggestions as to what I'm doing wrong?

TIA,

Richard

If all you are trying to do is extract the first integer value from the string,
then, simpler:

======================
Function ExtrNum(str As String)
Dim re As Object, mc As Object
Set re = CreateObject("vbscript.regexp")
re.Pattern = "\d+"
If re.test(str) = True Then
Set mc = re.Execute(str)
ExtrNum = mc(0).Value
End If
End Function
=======================

Or, if you want to extract all the digits from the string:

========================
Function ExtrNum2(str As String)
Dim re As Object
Set re = CreateObject("vbscript.regexp")
re.Global = True
re.Pattern = "\D"
ExtrNum2 = re.Replace(str, "")
End Function
===========================

Of course, if the value might be a decimal number, then you might want
something like:

====================
Function ExtrNum(str As String)
Dim re As Object, mc As Object
Set re = CreateObject("vbscript.regexp")
re.Pattern = "[\-+]?\b\d*\.?\d+\b"
If re.test(str) = True Then
Set mc = re.Execute(str)
ExtrNum = mc(0).Value
End If
End Function
=======================
--ron
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top