vbscript - instrRev reduce a string down

does anyone know how to reduce a string down with the instrRev or instr..dynamically...

Logical5=6015\:USD\:5\:6050@5@6017@6025@5@0@0\:0\:0\:0@false@-1_1@44@0@0@-1_1;5;x;0\:0\:0\:0;0;0


var1 = @5@6017@6025@5@0@0\:0\:0\:0@false@-1_1@44@0@0@-1_1;5;
var2 = ;0\:0\:0\:0;0;0

so that I can now modify the x value in that row...

newRowVar = "Logical5=6015\:USD\:5\:6050" & var1 & "900" & var2

thanks!!
LVL 10
GlobaLevelProgrammerAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Guy Hengel [angelIII / a3]Billing EngineerCommented:
is "x" the real value in there?
if yes, you might consider doing a
newRowVar = REPLACE(Logical5, ";x;", "900")

apart from that, it's far from obvious otherwise how to "locate" the x...
you might want to clarify the "rule(s)" ?
0
GlobaLevelProgrammerAuthor Commented:
hi angelIII...sorry x is not a real value I only put that in there so you can see where the value is located...its a number that change ...just like any other value in this row...
0
Guy Hengel [angelIII / a3]Billing EngineerCommented:
ok, but again: how exactly to locate "x" ?
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
The Ultimate Tool Kit for Technolgy Solution Provi

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy for valuable how-to assets including sample agreements, checklists, flowcharts, and more!

GlobaLevelProgrammerAuthor Commented:
I know ...thats what im asking.. ;)

var1 = Mid(origVar, InStrRev(origVar, "=") +1)
   msgbox(var1)
var2 = right(var1, InStrRev(var1, "\") +1)
   msgbox(var2)
var3 = right(var2, InStrRev(var2, "\") +1)
   msgbox(var3)
var4 = right(var3, InStrRev(var3, "\") +1)
   msgbox(var4)
var5 = (left(right(var4, InStrRev(var4, "@") +1)-1))
   msgbox(var5)

0
Guy Hengel [angelIII / a3]Billing EngineerCommented:
you might want to check split() function, but still not sure how you locate this exactly?
0
RobSampsonCommented:
Something like this might work. It's reasonably long winded, as opposed to using Split, but does the job.  It also works on the assumption that there is exactly the same amount of semi-colons in each line.

Regards,

Rob.
strLine = "Logical5=6015\:USD\:5\:6050@5@6017@6025@5@0@0\:0\:0\:0@false@-1_1@44@0@0@-1_1;5;x;0\:0\:0\:0;0;0"
var1 = ""
var2 = ""
strValue = ""
' Find the first ;
intPos = InStr(strLine, ";")
If intPos > 0 And intPos < Len(strLine) Then
	' Find the second ;
	intPos = InStr(intPos + 1, strLine, ";")
	var1 = Left(strLine, intPos)
	If intPos > 0 And intPos < Len(strLine) Then
		' Now find the next ; and get the value of x in between
		intPos2 = InStr(intPos + 1, strLine, ";")
		If intPos2 > 0 Then
			strValue = Mid(strLine, intPos + 1, (intPos2 - 1) - intPos)
			var2 = Mid(strLine, intPos2)
		End If
	End If
End If
MsgBox "var1: " & var1 & VbCrLf & "var2: " & var2 & VbCrLf & "strValue: " & strValue

Open in new window

0
RobSampsonCommented:
Here is the Split version of that, again based on the assumption that there are exactly the same amount of semi-colons in the line.

Regards,

Rob.
strLine = "Logical5=6015\:USD\:5\:6050@5@6017@6025@5@0@0\:0\:0\:0@false@-1_1@44@0@0@-1_1;5;x;0\:0\:0\:0;0;0"
var1 = ""
var2 = ""
strValue = ""
arrBits = Split(strLine, ";")
For intBit = 0 To 1
	var1 = var1 & arrBits(intBit) & ";"
Next
strValue = arrBits(2)
For intBit = 3 To 5
	var2 = var2 & ";" & arrBits(intBit)
Next
MsgBox "var1: " & var1 & VbCrLf & "var2: " & var2 & VbCrLf & "strValue: " & strValue

Open in new window

0
GlobaLevelProgrammerAuthor Commented:
turns out the '6050' is an unknown value and needs to stay dynamic...
was this:
newRowVar = "Logical5=6015\:USD\:5\:6050" & var1 & "900" & var2
needs to be this:
newRowVar = "Logical5=6015\:USD\:5\:" & var1 & "900" & var2
0
RobSampsonCommented:
OK, what about this?

Regards,

Rob.
strLine = "Logical5=6015\:USD\:5\:6050@5@6017@6025@5@0@0\:0\:0\:0@false@-1_1@44@0@0@-1_1;5;x;0\:0\:0\:0;0;0"
var1 = ""
var2 = ""
var3 = ""
strValue2 = ""
arrBits = Split(strLine, ";")
For intBit = 0 To 1
	var1 = var1 & arrBits(intBit) & ";"
Next
strValue2 = arrBits(2)
For intBit = 3 To 5
	var3 = var3 & ";" & arrBits(intBit)
Next
' Now find the first @ in var1
intEnd = InStr(var1, "@")
intStart = InStrRev(var1, ":", intEnd)
strValue1 = Mid(var1, intStart + 1, intEnd - 1 - intStart)
var2 = Mid(var1, intEnd)
var1 = Left(var1, intStart)

strNewRow = var1 & strValue1 & var2 & strValue2 & var3

MsgBox "var1: " & var1 & VbCrLf & "strValue1: " & strValue1 & VbCrLf & "var2: " & var2 & VbCrLf & "strValue2: " & strValue2 & VbCrLf & "var3: " & var3 & VbCrLf & VbCrLf & "strNewRow: " & VbCrLf & strNewRow

Open in new window

0
tv0085Commented:
Maybe it's better to do this with regular expressions.
You can use http://www.txt2re.com to create the regex to select x and the replace method to replace x with the new value. Select VBScript at the bottom to get the code generated.
replace method: http://msdn.microsoft.com/en-us/library/k9z80300(VS.85).aspx
0
GlobaLevelProgrammerAuthor Commented:
tv0085...can you give feedback as to what this regular expression generator actually does..thanks...
0
tv0085Commented:
Sorry, i did not have time to explain everything yesterday. First you have to place the string in the textbox and press the button. You can then click the things you like to retrieve using regex in the generator. It works well for simple strings but complicated ones need a little editing.
If you are sure x does not contain a ; you can use this code. Else I would use inStr and inStrRev to look for the second semicolon from the left and the first from the right.
  Dim txt
  txt ="Logical5=6015\:USD\:5\:6050@5@6017@6025@5@0@0\:0\:0\:0@false@-1_1@44@0@0@-1_1;5;This is what you would like to find. But there can't be a semicolon in here.;0\:0\:0\:0;0;0"

  Dim re
  re =".*?;.*?;(.*?);.*?"	'Non-greedy match on filler
    Dim r
  Set r = New RegExp
  r.Pattern = re
  r.IgnoreCase = True
  Dim m
  Set m = r.Execute(txt)
  If m.Item(0).SubMatches.Count > 0 Then
      Dim c1
      c1=m.Item(0).SubMatches.Item(0)
      msgbox c1
  End If

Open in new window

0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
VB Script

From novice to tech pro — start learning today.