View Single Post
  #5   Report Post  
Posted to microsoft.public.excel.programming
Ron Rosenfeld[_2_] Ron Rosenfeld[_2_] is offline
external usenet poster
 
Posts: 1,045
Default Find the first numeric substring

On Tue, 12 Apr 2011 02:16:39 -0700, "Robert Crandal" wrote:

Ooops, my mistake about that. Um, all my data samples
ONLY contain whole numbers or integer substrings, so there
will never be any floating point numbers.

To be even more precise, I am only looking for an ALL
numeric substring that has at least one whitespace character
on the left and at least one whitespace character on the right.

So, for example:

1) "cat dog11 23 tree carrot"
- should return "23"

2) "rock paper scissors 12b 100 pencil"
- should return "100"

I should have been more specific earlier, but I'm just now
realizing exactly what I need. Also, you can safely
assume that all my data will contain at least one number
substring that matches the above criteria or specs; there
will never be any data that is missing numbers padded by
whitespace characters.

Okay, I'll play with you code now and see if it still works
for my requirements.

Thanks Rick


Given those parameters, this modification of my original code should work:

============================
Option Explicit
Function ExtrDigits(s As String) As String
Dim re As Object, mc As Object
Const sPat As String = "\s(\d+)\s"

Set re = CreateObject("vbscript.regexp")
re.Global = False
re.Pattern = sPat

If re.test(s) Then
Set mc = re.Execute(s)
ExtrDigits = mc(0).submatches(0)
End If

End Function
==========================