bash 检查有效链接(URL)

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3183444/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-17 22:17:26  来源:igfitidea点击:

Check for valid link (URL)

bashurlhyperlinkvalidation

提问by Mint

I was reading though this other questionwhich has some really good regex's for the job but as far as I can see non of them work with BASH commands as BASH commands don't support such complex rexeg's.

我正在阅读另一个问题,该问题对这项工作有一些非常好的正则表达式,但据我所知,它们中没有一个可以使用 BASH 命令,因为 BASH 命令不支持如此复杂的正则表达式。

if echo "http://www.google.com/test/link.php" | grep -q '(https?|ftp|file)://[-A-Z0-9\+&@#/%?=~_|!:,.;]*[-A-Z0-9\+&@#/%=~_|]'; then 
    echo "Link valid"
else
    echo "Link not valid"
fi

But this doesn't work as grep -q doesn't work ...

但这不起作用,因为 grep -q 不起作用......

Edit, ok I just realised that grep had an "extended-regex" (-E) option which seems to make it work. But if anyone has a better/faster way I would still love to here about it.

编辑,好吧,我刚刚意识到 grep 有一个“扩展正则表达式”(-E)选项,这似乎使它起作用。但是如果有人有更好/更快的方法,我仍然很乐意在这里讨论它。

回答by Paused until further notice.

The following works in Bash >= version 3.2 without using grep:

以下适用于 Bash >= 3.2 版,无需使用grep

regex='(https?|ftp|file)://[-A-Za-z0-9\+&@#/%?=~_|!:,.;]*[-A-Za-z0-9\+&@#/%=~_|]'
string='http://www.google.com/test/link.php'
if [[ $string =~ $regex ]]
then 
    echo "Link valid"
else
    echo "Link not valid"
fi

Your regex doesn't seem to include lowercase alpha characters [a-z]so I added them here.

您的正则表达式似乎不包含小写字母字符,[a-z]因此我在此处添加了它们。

回答by Patrick Steil

Since I don't have enough rep to comment above, I am going to amend the answer given by Dennis above with this one.

由于我没有足够的代表在上面发表评论,我将用这个来修改上面丹尼斯给出的答案。

I incorporated Christopher's update to the regex and then added more to it so that the URL has to at least be in this format:

我将 Christopher 的更新合并到正则表达式中,然后添加了更多内容,以便 URL 至少必须采用以下格式:

http://w.w(has to have a period in it).

http://ww(必须有句号)。

And tweaked output a bit :)

并稍微调整了输出:)

regex='^(https?|ftp|file)://[-A-Za-z0-9\+&@#/%?=~_|!:,.;]*[-A-Za-z0-9\+&@#/%=~_|]\.[-A-Za-z0-9\+&@#/%?=~_|!:,.;]*[-A-Za-z0-9\+&@#/%=~_|]$'

url='http://www.google.com/test/link.php'
if [[ $url =~ $regex ]]
then 
    echo "$url IS valid"
else
    echo "$url IS NOT valid"
fi

回答by David Z

Probably because the regular expression is written in PCRE syntax. See if you have (or can install) the program pcregrepon your system - it has the same syntax as grepbut accepts Perl-compatible regexes - and you should be able to make that work.

可能是因为正则表达式是用 PCRE 语法编写的。查看pcregrep您的系统上是否有(或可以安装)该程序- 它的语法与grepPerl 兼容的正则表达式相同,但您应该能够使其工作。

Another option is to try the -Poption to grep, but the man page says that's "highly experimental" so it may or may not actually work.

另一种选择是尝试-P选择grep,但手册页说这是“高度实验性的”,因此它可能会也可能不会实际工作。

I will say that you should think carefully about whether it's really appropriate to be using this or any regex to validate a URL. If you want to have a correct validation, you'd probably be better off finding or writing a small script in, say, Perl, to use the URL validation facilities of the language.

我会说你应该仔细考虑使用这个或任何正则表达式来验证 URL 是否真的合适。如果您想进行正确的验证,您最好找到或编写一个小脚本,例如 Perl,以使用该语言的 URL 验证工具。

EDIT: In response to your edit in the question, I didn't notice that that regex is also valid in "extended" syntax. I don't think you can get better/faster than that.

编辑:为了回应您在问题中的编辑,我没有注意到该正则表达式在“扩展”语法中也有效。我认为你不能比这更好/更快。