60

I am framing a regex to check if a word starts with http:// or https:// or ftp://, my code is as follows,

     public static void main(String[] args) {
    try{
        String test = "http://yahoo.com";
        System.out.println(test.matches("^(http|https|ftp)://"));
    } finally{

    }
}

It prints false. I also checked stackoverflow post Regex to test if string begins with http:// or https://

The regex seems to be right but why is it not matching?. I even tried ^(http|https|ftp)\:// and ^(http|https|ftp)\\://

Community
  • 1
  • 1
Abhishek
  • 6,862
  • 22
  • 62
  • 79

6 Answers6

107

You need a whole input match here.

System.out.println(test.matches("^(http|https|ftp)://.*$")); 

Edit:(Based on @davidchambers's comment)

System.out.println(test.matches("^(https?|ftp)://.*$")); 
Community
  • 1
  • 1
Prince John Wesley
  • 62,492
  • 12
  • 87
  • 94
35

Unless there is some compelling reason to use a regex, I would just use String.startsWith:

bool matches = test.startsWith("http://")
            || test.startsWith("https://") 
            || test.startsWith("ftp://");

I wouldn't be surprised if this is faster, too.

Randall Cook
  • 6,728
  • 6
  • 33
  • 68
  • 6
    I'd be surprised if this wasn't slower (compared to a compiled regex that is), but have to test to find out. – Joel Nov 17 '15 at 19:00
5

If you wanna do it in case-insensitive way, this is better:

System.out.println(test.matches("^(?i)(https?|ftp)://.*$")); 
user1079877
  • 9,008
  • 4
  • 43
  • 54
4

I think the regex / string parsing solutions are great, but for this particular context, it seems like it would make sense just to use java's url parser:

https://docs.oracle.com/javase/tutorial/networking/urls/urlInfo.html

Taken from that page:

import java.net.*;
import java.io.*;

public class ParseURL {
    public static void main(String[] args) throws Exception {

        URL aURL = new URL("http://example.com:80/docs/books/tutorial"
                           + "/index.html?name=networking#DOWNLOADING");

        System.out.println("protocol = " + aURL.getProtocol());
        System.out.println("authority = " + aURL.getAuthority());
        System.out.println("host = " + aURL.getHost());
        System.out.println("port = " + aURL.getPort());
        System.out.println("path = " + aURL.getPath());
        System.out.println("query = " + aURL.getQuery());
        System.out.println("filename = " + aURL.getFile());
        System.out.println("ref = " + aURL.getRef());
    }
}

yields the following:

protocol = http
authority = example.com:80
host = example.com
port = 80
path = /docs/books/tutorial/index.html
query = name=networking
filename = /docs/books/tutorial/index.html?name=networking
ref = DOWNLOADING
gdiz
  • 51
  • 1
  • 1
1

test.matches() method checks all text.use test.find()

shift66
  • 11,760
  • 13
  • 50
  • 83
0

Add a verification between startsWith and matches.

import java.net.URL


fun main(args: Array<String>) {
    
    val IMAGE_SERVER = "https://google.com/file/"
    val IMAGE_REG: String by lazy {
        val url = URL(IMAGE_SERVER)
        "^(http|https)://${url.host}${url.path}.*\$"
    }
    
    val regx = Regex(IMAGE_REG)
    println(IMAGE_REG)
    

    var begin = System.nanoTime()
    var aa = IMAGE_SERVER.startsWith(IMAGE_SERVER)
    println("startsWith:"+ (System.nanoTime()-begin))
    println("startsWith:"+ aa)
    
    begin = System.nanoTime()
    aa = IMAGE_SERVER.matches(regx)
    println("matches:"+ (System.nanoTime()-begin))
    println("matches:"+ aa)
    

}

startsWith:3755625
startsWith:true
matches:174250
matches:true

matches is 174us, startswith is 3.755ms

matches is much better than startsWith on performance and code cleanness in the scenario.

Victor Choy
  • 4,006
  • 28
  • 35