-3

I'm currently trying to check the user input to be a valid URL (http(s)://www.abc.at or localhost) or a valid IP-Address (127.0.0.1, ...) using only the standard C++ methods like REGEX. I could use the libraries ASIO (standalone), regex and arpa/inet.h. Is there a way to do that in very simple ways?

Thanks for your help!

Blackbird
  • 23
  • 1
  • 7
  • 2
    There are gazillion ways to do that, yes. – πάντα ῥεῖ Feb 11 '17 at 08:53
  • The regex is not standart c++ methods. Try to integrate library to your build. Try to use some in code. If you will have concrete problem come back and ask question. Do not forget to search answer before that. – oklas Feb 11 '17 at 08:55
  • 4
    @oklas Of course [`std::regex`](http://en.cppreference.com/w/cpp/regex/basic_regex) is part of the standard. Under what rock you're living? – πάντα ῥεῖ Feb 11 '17 at 08:59
  • Of course it standard since C++11. It came out of attention. Seems I even have use that. Hm... – oklas Feb 11 '17 at 09:09
  • 2
    Checking a valid URL is... hard. See [URL parsing in WebKit](https://webkit.org/blog/7086/url-parsing-in-webkit/). – tambre Feb 11 '17 at 09:14
  • This is a bit suspicious. The usual alternative to an IP address is a DNS name (because DNS allows you to translate the name back to an IP address). Hence, a regex that accepts either an IP address or a DNS name is common. On a somewhat related note, don't forget IPv6. – MSalters Feb 11 '17 at 12:27

2 Answers2

3

Usually you should be wary about doing such validations yourself because they tend to be a lot more complicated than it appears on first glance. For example to validate an IPv4 you cannot just check for “4 numbers separated by dots”. You’ll also have to check things like the range of each number (0-255), special cases like 0.0.0.0, etc. Then what about IPv6? URLs/hostnames aren’t any less complex.

To answer your concrete question: No, there is no simple way to validate an IP/hostname.

Either use a dedicated library for checking or simply try to do whatever it is you want to do with the address and handle errors appropriately. You might consider doing a rough sanity check for obvious errors in the beginning, mainly to provide better error messages to the user. But even that requires a bit of thought. For example, it’s easy to forget about IPv6 and reject perfectly valid addresses.

besc
  • 2,507
  • 13
  • 10
  • Exactly. You should use whatever API you're going to use to actually convert the input to something useable with `connect()` or whatever you're going to call, and handle the errors from that API as they arise. Don't try to anticipate API errors. – user207421 Feb 11 '17 at 09:50
0

Just use this:

^https?:\/\/(.+\..{2,10}|localhost|(?:\d{1,3}\.){3}\d{1,3})\/?.*?$

This will match any address which starts with https:// or http://, followed by one of the following three cases:

  1. any characters, followed by a dot . and a TLD with length of 2 up to 10.
  2. localhost
  3. an IP address with 4 segments of numbers with up to 3 characters (does not check the validity of the IP address, does accept 999.999.999.999.

Here is a live example.

ssc-hrep3
  • 15,024
  • 7
  • 48
  • 87