Java Regex URL Validate isUrlAddress(String text)

Here you can find the source of isUrlAddress(String text)

Description

is Url Address

License

Open Source License

Declaration

@SuppressWarnings("nls")
    public static boolean isUrlAddress(String text) 

Method Source Code

//package com.java2s;
/* ******************************************************************************
 * Copyright (c) 2006-2012 XMind Ltd. and others.
 * /*  w  w w . jav a 2  s . c  o m*/
 * This file is a part of XMind 3. XMind releases 3 and
 * above are dual-licensed under the Eclipse Public License (EPL),
 * which is available at http://www.eclipse.org/legal/epl-v10.html
 * and the GNU Lesser General Public License (LGPL), 
 * which is available at http://www.gnu.org/licenses/lgpl.html
 * See http://www.xmind.net/license.html for details.
 * 
 * Contributors:
 *     XMind Ltd. - initial API and implementation
 *******************************************************************************/

import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class Main {
    @SuppressWarnings("nls")
    public static boolean isUrlAddress(String text) {
        final String URL_REGEX = "^(https?|ftp|file)://[-a-zA-Z0-9+&@#/%?=~_|!:,.;]*[-a-zA-Z0-9+&@#/%=~_|]";
        Pattern pattern = Pattern.compile(URL_REGEX);
        Matcher matcher;
        if (!text.contains("http://") && !text.contains("https://")
                && !text.contains("file://")) {
            matcher = pattern.matcher("http://" + text);
        } else {
            matcher = pattern.matcher(text);
        }
        return matcher.matches() && isLinkToWeb(text);
    }

    @SuppressWarnings("nls")
    public static boolean isLinkToWeb(String urlOrBookmark) {
        if (urlOrBookmark.contains("www.")
                || urlOrBookmark.contains(".com")
                || urlOrBookmark.contains(".cn")
                || urlOrBookmark.contains(".org")
                || urlOrBookmark.contains(".cc")
                || urlOrBookmark.contains(".net")) {
            return true;
        }
        return false;
    }
}

Related

  1. isUrl(String text)
  2. isURL(String url)
  3. isUrl(String url)
  4. isURL(String url)
  5. isUrl(String x)
  6. isUrlFile(String filePath)
  7. isUrlWithUserInfo(String str)
  8. isUrn(String value)