AutoHotkey:测试字符串是否为URL

时间:2011-05-12 18:48:41

标签: url substring autohotkey

好的,所以我有这个AutoHotkey片段:

; Google text from any app
; from http://superuser.com/questions/7271/most-useful-autohotkey-scripts/165220#165220
#s::
MyClip := ClipboardAll
clipboard = ; empty the clipboard
Send, ^c
ClipWait, 2
if ErrorLevel  ; ClipWait timed out.
    return
Run http://www.google.com/#hl=en&q=%clipboard%
Clipboard := MyClip

它工作正常,但我想改进它以处理突出显示网址并自动运行网址(Run %clipboard%)而不是搜索Google的情况。如何让AutoHotkey检测字符串是否为URL?

似乎我可以使用StringLeftSubStr来提取前几个字符并查看它们是否与http://www.匹配,或者使用正则表达式可能更强大?但我并不真正理解AHK的语法。

This script检查剪贴板中的URL,显然是使用StringGetPos,但我不想检测www是否在字符串中出现 。只有它出现在开头。

1 个答案:

答案 0 :(得分:4)

这是您的脚本,其代码用于检查复制的文本是否是通过正则表达式的URL。如果它是一个URL,它只运行文本,否则它使用搜索谷歌的旧行为来复制文本。

#s::
MyClip := ClipboardAll
clipboard = ; empty the clipboard
Send, ^c
ClipWait, 2
if ErrorLevel  ; ClipWait timed out.
    return
If RegExMatch(Clipboard, "^(https?://|www\.)[a-zA-Z0-9\-\.]+\.[a-zA-Z]{2,3}(/\S*)?$")
    Run %Clipboard%
Else
    Run http://www.google.com/#hl=en&q=%clipboard%
Clipboard := MyClip

修改:更改代码以匹配以“www”开头的网址。