vbscript使用InStr查找URL中不同的信息

时间:2013-12-04 15:02:53

标签: url vbscript

我有一个项目,用户在该项目中提取一个特定的URL,其中Dept,Queue和Day的值会根据他们选择的超链接而改变。例如,他们会点击一个超链接,URL就像:

http://www.myworkplace.com/UserPlatform/User/Process.aspx?Dept=DeptOne&Queue=18&Day=0

下一个超链接可能是:

http://www.myworkplace.com/UserPlatform/User/Process.aspx?Dept=DeptFive&Queue=13&Day=9

我想使用InStr在网址中查找Dept,Queue和Day,然后将其值设置为变量,例如UDeptUQueue和{{1 }}。然后,根据这些值,用户可以复制只能在具有这些值的URL上找到的特定ID号。最终结果是搜索URL:

UDay

到目前为止,这是我的代码:

http://www.myworkplace.com/UserPlatform/User/Process.aspx?Dept=UDept&Queue=UQueue&Day=UDay

提前感谢任何可以帮助我的人。

2 个答案:

答案 0 :(得分:0)

您是否考虑使用正则表达式?

dim re, s
dim matches

s = "http://www.myworkplace.com/UserPlatform/User/Process.aspx?Dept=DeptFive&Queue=13&Day=9"

Set re = new RegExp
re.Pattern = ".*?Dept=(\w+)&Queue=(\d+)&Day=(\d+)$"
Set matches = re.Execute(s)

Dim uDept, uQueue, uDay
uDept = matches(0).submatches(0)
uQueue = matches(0).submatches(1)
uDay = matches(0).submatches(2)

Msgbox join(array("uDept = " & uDept, "uQueue = " & uQueue , "uDay = " & uDay), vbNewLine)

' Output:
' uDept = DeptFive
' uQueue = 13
' uDay = 9

要替换您还可以使用正则表达式:

Set re = new RegExp
s = "http://www.myworkplace.com/UserPlatform/User/Process.aspx?Dept=DeptFive&Queue=13&Day=9"
newDept = "DeptFourtyTwo"
newQueue = 404
newDay = 12

re.Pattern = "(Dept=)\w+"
newUrl = re.Replace(s, "$1" & newDept)
re.Pattern = "(Queue=)\d+"
newUrl = re.Replace(newUrl, "$1" & newQueue)
re.Pattern = "(Day=)\d+"
newUrl = re.Replace(newUrl, "$1" & newDay)

msgbox newUrl
' output:
' http://www.myworkplace.com/UserPlatform/User/Process.aspx?Dept=DeptFourtyTwo&Queue=404&Day=12

' Alternatively you can replace everything at once if the order and presence of
' parameters is guaranteed:
re.Pattern = "(Dept=)\w+(&Queue=)\d+(&Day=)\d+"
MsgBox re.Replace(s, "$1DeptFourtyTwo$2404$312")     

答案 1 :(得分:0)

这仅使用Instr和Mid Function的

s="http://www.myworkplace.com/UserPlatform/User/Process.aspx?Dept=DeptFive&Queue=13&Day=9"

  a = InStr(s, "?") 'We get the value until ?

    d1 = Mid(s, a)

    c1 = InStr(d1, "=")

    c2 = InStr(d1, "&")

    d2 = Mid(d1, c2 + 1)
    d3 = Mid(d1, c1 + 1, (c2 - c1) - 1) 'value of Dept is d3



    c3 = InStr(d2, "=")
    c4 = InStr(d2, "&")
    d5 = Mid(d2, c4 + 1)

    d4 = Mid(d2, c3 + 1, (c4 - c3) - 1) 'value of Queue is d4

    c6 = InStr(d5, "=")
    d6 = Mid(d5, c6 + 1) ' Value of Day is d6

希望这有帮助