我的 QTP 测试项目中有一个字符串。在某些情况下,该字符串是明文电子邮件的内容;在其他情况下,它是 HTML。在这两种情况下,我都需要从字符串中删除所有 URL 以将其与预期的情况相匹配。
这如何在 QTP/VBScript 中完成?
这应该可以解决问题,尽管您的 URL 需要以 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
匹配 URL 的正则表达式模式来自Chris Freyer 的博客,似乎可以处理您可能遇到的大多数类型的 URL。它在我用它进行的测试中运行良好。