0

For example, I have this string that reads "IRS150Sup2500Vup". It could also be "IRS250Sdown1250Vdown".

In my previous qn, I asked how to find a number between 2 characters. Now, I need to find the word up or down after the second S now. Since it appears between the character S and the number, how do I do it?

My code looks like this:

Dim pos, pos1,pos2 strString As String
pos = InStr(1, objFile.Name, "S") + 1
pos1 = InStr(pos, objFile.Name, "S")
pos2 = InStr(pos1, objFile.Name, ?)

pos1 returns the index of the second S. I am not sure what to place in ?

Community
  • 1
  • 1
lakshmen
  • 28,346
  • 66
  • 178
  • 276

1 Answers1

1

Using Regex.

Note: you need a reference to MS VBScripts Regular Expression library.

Dim r As VBScript_RegExp_55.RegExp
Dim sPattern As String, myString As String
Dim mc As VBScript_RegExp_55.MatchCollection, m As VBScript_RegExp_55.Match

myString = "IRS150Sup2500Vup"
sPattern = "\w?up+" 'searches for Sup, Vup, etc.
Set r = New VBScript_RegExp_55.RegExp
r.Pattern = sPattern

Set mc = r.Execute(myString)
For Each m In mc ' Iterate Matches collection.
    MsgBox "word: '" & m.Value & "' founded at: " & m.FirstIndex & " length: " & m.Length
Next

For further information, please see:
How To Use Regular Expressions in Microsoft Visual Basic 6.0
Find and replace text by using regular expressions (Advanced)

Maciej Los
  • 8,468
  • 1
  • 20
  • 35