QTP / VBScript:如何从字符串中删除所有URL?

时间:2009-04-21 12:25:09

标签: regex vbscript replace qtp

我的QTP测试项目中有一个字符串。在某些情况下,此字符串是明文电子邮件的内容;在其他情况下,它是HTML。在这两种情况下,我都需要从字符串中删除所有URL以匹配Expected案例。

如何在QTP / VBScript中完成?

1 个答案:

答案 0 :(得分:1)

这应该可以解决问题,但您的网址需要以http://或https://开头才能获取:

Dim text
text = "<your text with URLs here>"

Dim rgx
Set rgx = New RegExp
rgx.IgnoreCase = True
rgx.Global = True
rgx.Pattern = "([A-Za-z]{3,9})://([-;:&=\+\$,\w]+@{1})?([-A-Za-z0-9\.]+)+:?(\d+)?((/[-\+~%/\.\w]+)?\??([-\+=&;%@\.\w]+)?#?([\w]+)?)?"

Dim match, matches
Set matches = rgx.Execute(text)
For Each match in matches
  MsgBox match.Value, 0, "Found Match"
Next

匹配网址的正则表达式模式来自Chris Freyer's blog,似乎可以处理您可能遇到的大多数类型的网址。它在我用它进行的测试中运行良好。