ExcelBanter

ExcelBanter (https://www.excelbanter.com/)
-   Excel Programming (https://www.excelbanter.com/excel-programming/)
-   -   Problem with RegEx and matches (https://www.excelbanter.com/excel-programming/412729-problem-regex-matches.html)

Richard L Rosenheim

Problem with RegEx and matches
 
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



Richard L Rosenheim

Problem with RegEx and matches
 
Dana,

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

Richard


"Dana DeLouis" wrote in message
...
Hi. I believe you need to use "SubMatches".
One technique is to look at the "Locals Window" when stepping thru the

code...

MsgBox "Match : " & match.SubMatches(0)

An alternative is to remove anything that is not a digit.
This doesn't have any testing for more than 1 digit group.

Sub Demo()
Dim RegEx
Dim Str As String
Const PatternNetTerms = "\D+"

Str = "Net 10 Days"

Set RegEx = CreateObject("VbScript.RegExp")
RegEx.Pattern = PatternNetTerms
RegEx.IgnoreCase = True
RegEx.Global = True
RegEx.MultiLine = True

If RegEx.test(Str) Then
Str = RegEx.Replace(Str, vbNullString)
MsgBox Str
End If
End Sub



--
HTH :)
Dana DeLouis


"Richard L Rosenheim" wrote in message

...
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






All times are GMT +1. The time now is 11:57 AM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
ExcelBanter.com