RegEx to extract a number from a text string, based on its position in the string.

geodekl

New Member
Joined
Nov 6, 2013
Messages
6
I use a fairly simple vba routine to extract data from a table contained in an Outlook email body. The result is an array which contains 1 entry with 7 columns for each email.
Here is a sample of one entry in the array:

arrList(1,1) = "Sent: Monday, August 28, 2017 10:07:28 AM (UTC-06:00) Central Time (US & Canada)"
arrList(1,2) = "Total Chats 10 7 "
arrList(1,3) = "Chats Completed 7 5 "
arrList(1,4) = "Chats Dropped 2 1 "
arrList(1,5) = "Chats Missed 1 1 "
arrList(1,6) = "Offline Messages 2 2 "
arrList(1,7) = "Avg Chat Duration 5m 15s 9m 13s "

Once the array is filled, I have to clean it up:

Just the date for column 1:
arrList(1,1) = "8/28/2017"

The first number in the set for columns 2-6:
arrList(1,2) = "10"
arrList(1,3) = "7"
arrList(1,4) = "2"
arrList(1,5) = "1"
arrList(1,6) = "2"

The total time in seconds from the first minute/second pair for column 7:
arrList(1,7) = "315" (5 minutes x 60 seconds plus 15 seconds)
(Note - if either pair is "0m 0s", it will be replaced by a dash: "- 9m 13s" instead of "0m 0s 9m 13s"

Right now I either do this using formulas in an excel spreadsheet, or by doing a series of manual operations on the data (CTL+H to strip out the text parts, for example).

I want to edit my VBA code so it cleans each element before loading it into the array. I can handle this with a combination of string operations (replace, split, test, join, etc.), but it takes a few steps for each element.

Which is going to be more efficient? A set of string operations on each element, or a RegEx to extract the data I need from each element? How do I determine whether the string contains tabs, spaces, or some other kind of separator?

Can you point me at a page which clearly explains how to build the RegEx if that's the best option? :confused:
(Note - typically this will be used with a pretty small data-set, so being easy to understand and modify is probably more important than being as fast as possible).

Thanks,

-geodekl
 

Excel Facts

Pivot Table Drill Down
Double-click any number in a pivot table to create a new report showing all detail rows that make up that number
On internet you can find patterns for different situations.
This delete text from left include space: "^[A-Z-a-z]*(\s)"
 
Upvote 0

Forum statistics

Threads
1,223,246
Messages
6,170,996
Members
452,373
Latest member
TimReeks

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top