user2144099
user2144099

Reputation: 33

Regex to match hosts with or without http and https ;

I am beginner to Java and I have been trying to figure out a regex that matches the following:

  1. A host only and not an IP
  2. the host might be with an http or https tag as well.
  3. The host can have different TLDs.

i tried [a-z0-9]+|([a-z0-9]+[-]+[a-z0-9]+))[.])+ but I am not getting what I expected. It would be great if someone could help me on this.

Upvotes: 0

Views: 306

Answers (2)

Mikhail Vladimirov
Mikhail Vladimirov

Reputation: 13890

public static void main(String[] args) {
Pattern pattern = Pattern.compile ("(?:https?://)?(?:[-a-zA-Z0-9_]+\\.)*[-a-zA-Z0-9_]*[-a-zA-Z_][-a-zA-Z0-9_]*(?:\\.[-a-zA-Z0-9_]+)*");

System.out.println(pattern.matcher("127.0.0.1").matches()); // false
System.out.println(pattern.matcher("1.0.0.127.in-addr.arpa").matches()); // true
System.out.println(pattern.matcher("localhost").matches()); // true
System.out.println(pattern.matcher("1-2-3-4").matches()); // true
System.out.println(pattern.matcher("http://1.0.0.127").matches()); // false
System.out.println(pattern.matcher("https://1.0.0.127").matches()); // false
System.out.println(pattern.matcher("ftp://1.0.0.127.in-addr.arpa").matches()); // false
System.out.println(pattern.matcher("http://1.0.0.127.in-addr.arpa").matches()); // true
System.out.println(pattern.matcher("https://1.0.0.127.in-addr.arpa").matches()); // true

Upvotes: 2

Raffaele
Raffaele

Reputation: 20885

Not sure what you're trying to accomplish. You want the host portion, but only a resolved one, ie IP addresses are not allowed. Why do you ever want it? And what about the optional port number? And what about possible login information? My suggestion is using a java.net.URL (or URI) object if you want to do anything serious with the parsed data.

If you only want to play with regular expression, please specify your requirements better, because depending on the input text, no regex may be enough. Consider the following input:

Lorem ipsum.Dolor sit amen

Should ipsum.Dolor be accepted as a valid host? It does not have the http:// protocol spec and is not an IP address, so according to your requirements it should match.

Upvotes: 0

Related Questions