• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 885
  • Last Modified:

Instr Like Function in Visual Basic 6

I need to know how to get the starting position in a string using masks you use in LIKE functions.  Let me clarify.

Say I have a string like "ABCDEFG123MNOPQ"

I want the starting position where the string is like ###.  I don't want any Mid functions because that could take a long time to scan huge strings position by position.  Does VB6 have a built in function or is there another way?

Instr("ABCDEFG123MNOPQ","123") will return my starting postion

"ABCDEFG123MNOPQ" LIKE "*###*" will return if that mask combination exists.

I need the combination of both.

Thanks in advance.
0
tw_chase
Asked:
tw_chase
1 Solution
 
Guy Hengel [angelIII / a3]Billing EngineerCommented:
0
 
mvidasCommented:
I agree with angel, regular expressions would definately be the way to go, he deserves the majority if not all of the points for it.  If it helps, heres an example for you:

Sub tw_chase()
 Dim RegEx As Object, RegM As Object, tempStr As String, tempStr2 As String
 Set RegEx = CreateObject("vbscript.regexp")
 RegEx.Pattern = "\d{3}"
 tempStr = "ABCDEFG123MNOPQ"
 tempStr2 = "as2lkasdfo34klalsd872alsd"
 If RegEx.Test(tempStr) Then
  Set RegM = RegEx.Execute(tempStr).Item(0)
  MsgBox "Original string: " & tempStr & vbCrLf & _
         """" & RegM & """ found at position " & RegM.FirstIndex + 1
 End If
 If RegEx.Test(tempStr2) Then
  Set RegM = RegEx.Execute(tempStr2).Item(0)
  MsgBox "Original string: " & tempStr2 & vbCrLf & _
         """" & RegM & """ found at position " & RegM.FirstIndex + 1
 End If
 Set RegEx = Nothing
 Set RegM = Nothing
End Sub

Matt
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Tackle projects and never again get stuck behind a technical roadblock.
Join Now