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

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!!
0
GlobaLevel
Asked:
GlobaLevel
  • 4
  • 3
  • 3
  • +1
3 Solutions
 
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
 
GlobaLevelAuthor 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
Never miss a deadline with monday.com

The revolutionary project management tool is here!   Plan visually with a single glance and make sure your projects get done.

 
GlobaLevelAuthor 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
 
GlobaLevelAuthor 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
 
GlobaLevelAuthor 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

Featured Post

The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

  • 4
  • 3
  • 3
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now