20

I was reading though this other question which 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.

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 ...

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.

4
  • You might want to examine your motivation for asking this question. Programmers try to do static evaluation of URL/URIs and e-mail addresses which you can show to be syntactically valid but tells you nothing about their accessibility. For example, http://www.example.com/bogus#fragment will always be syntactically valid and will (presumably) always return a 404 error.
    – msw
    Commented Jul 6, 2010 at 4:38
  • This function doesn't need to know if the links works or not, just that it is a link.
    – Mint
    Commented Jul 6, 2010 at 4:55
  • Of what use is a URL that never locates a resource? Why even bother checking?
    – msw
    Commented Jul 6, 2010 at 10:32
  • E.g. if you want to evaluate if a check is needed at some input because it seems to be a URL
    – rubo77
    Commented Jun 26, 2022 at 4:49

3 Answers 3

36

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

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

I simplified your regex by using [:alnum:] which also matches any alphanumeric character (e.g. Э or ß), but support varies by the underlying regex library. This is another potential simplification which uses + instead of * and a repeated sequence (although your second sequence is different from the first).

regex='(https?|ftp|file)://[-[:alnum:]\+&@#/%?=~_|!:,.;]+'
7
  • 6
    Bug with http://печки-лавочки.рф/ which is a valid URL. A more complete regex can be found at stackoverflow.com/questions/161738 Commented Dec 3, 2013 at 9:35
  • 2
    This was helpful. But I think you want to anchor the regex to avoid a string like 'garbage http://google.com' being passed as valid. I just added ^ and $ to the beginning and end of the regex respectively, like so: regex='^(https?|ftp|file)://[-A-Za-z0-9\+&@#/%?=~_|!:,.;]*[-A-Za-z0-9\+&@#/%=~_|]$' Commented Oct 2, 2016 at 21:14
  • 1
    @rubo77: That can produce many false positives. Some, but not all, of these and many nonsensical strings. It lacks specificity and could match URL schemes that one wants to purposely exclude. A much better short regex would be to use mine up to the two slashes: ^(https?|ftp|file):// which is only slightly longer than yours. That can be easily modified to include or exclude any scheme. Thus it has high maintainability. Commented Jun 24, 2022 at 16:11
  • 1
    The answer die not include (allowed) special characters like the German ä,...
    – rubo77
    Commented Jun 25, 2022 at 10:01
  • 2
    @rubo77: I had simply copied the OP's regex. I have edited my answer so it now supports a larger character set. Commented Jun 25, 2022 at 12:59
5

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:

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

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
1

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

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

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.

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.

2
  • This is only the backend, more validation will be done in php before anything gets displayed.
    – Mint
    Commented Jul 6, 2010 at 4:56
  • Also not all implementations of grep even have the -P option.
    – Pryftan
    Commented Dec 22, 2022 at 18:39

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.