Home |
Search |
Today's Posts |
#1
![]()
Posted to microsoft.public.excel.programming
|
|||
|
|||
![]()
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 |
#2
![]()
Posted to microsoft.public.excel.programming
|
|||
|
|||
![]()
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 |
Reply |
Thread Tools | Search this Thread |
Display Modes | |
|
|
![]() |
||||
Thread | Forum | |||
Another Problem with Regex Pattern | Excel Programming | |||
Help with a Regex Pattern | Excel Programming | |||
Regex Capture problem | Excel Programming | |||
RegEx to parse something like this... | Excel Programming | |||
Regex Question | Excel Programming |