Need help with Regular Expression

ScottInTexas

Board Regular
Joined
Oct 28, 2003
Messages
178
I have had a hard time finding a site that explained the construction of a regular expression. But they all seem to copy each other and leave "Capture Group" vague. So this is a possible string from which I want to extract the words. I will type the tabs as (tab) so you know where they are. I want to capture the word groups between tabs. Some groups may contain numbers also.

"The Filename(tab)Some Multiplier 1(tab)Some Amount(tab)name1(tab)name2(tab)or name(tab)another N-1234"

I tried this pattern, \t?(\b\w*\b\s\b\w*\b)\t, and got;

Match 1 = The Filename (this is correct)
Match 2 = Multiplier 1 (it left out the "Some")
Match 3 = Some Amount (this is correct)
Match 4 = name1 name2 (but there is a tab character between the two)
Match 5 = or name (this is correct)

completely blew off the name N-1234

It's close but I don't know how to finish it.

Thanks for your help.
 

Excel Facts

Enter current date or time
Ctrl+: enters current time. Ctrl+; enters current date. Use Ctrl+: Ctrl+; Enter for current date & time.
Why not just use Split with tab as the separator?
 
Upvote 0
Hi

Try this code:

Code:
Sub SpitAtTab()
Dim regex As Object, regexMatches As Object
Dim s As String, lMatch As Long

s  = "The Filename" & vbTab & "Some Multiplier 1" & vbTab  & "Some Amount" & vbTab & "name1" & vbTab & "name2"  & vbTab & "or name" & vbTab & "another N-1234"

Set regex = CreateObject("VBScript.RegExp")
With regex
    .Pattern = ".+?(?=\t)"
    .Global = True
    Set regexMatches = .Execute(s & vbTab)
End With

' display matches
For lMatch = 0 To regexMatches.Count - 1
     MsgBox "Match " & (lMatch + 1) & ": " & regexMatches(lMatch)
Next lMatch

End Sub
 
Upvote 0
Hi

Sorry, the code I posted displays the results OK but will sometimes include the tab, which you don't want.

Replace the pattern with

Code:
 .Pattern = "[^\t]+"

to get any sequence of characters between 2 tabs
 
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,181
Members
453,022
Latest member
Mohamed Magdi Tawfiq Emam

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