Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 2
Default 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


  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 2
Default 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




Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Another Problem with Regex Pattern ExcelMonkey Excel Programming 2 May 31st 08 02:53 PM
Help with a Regex Pattern [email protected] Excel Programming 11 April 30th 07 01:49 AM
Regex Capture problem Dave Runyan Excel Programming 6 April 27th 07 02:01 AM
RegEx to parse something like this... R Avery Excel Programming 2 March 7th 05 06:41 PM
Regex Question William Barnes Excel Programming 5 January 2nd 04 11:57 AM


All times are GMT +1. The time now is 02:53 AM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
Copyright ©2004-2025 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"