"Java" uses regular expressions to determine whether URLs are

Source: Internet
Author: User

This article and the "JavaScript" using regular expressions to check the input box input is the URL (click to open the link) as a sister, as mentioned above, the regular expression in the various programs are generic, here no longer explain the specific use of regular expressions. Focus on how regular expressions are used in Java, or whether they are URLs.

The following four test cases are available:


The result of the operation is as shown.

The code is as follows:

Package Urlreg;import java.util.regex.*;p ublic class Urlregtest {public static void main (string[] args) {String URL1 = "HT Tp://www.xx.com "; String url2 = "w.xx.com"; String url3 = "http://w.xx.com"; String url4 = "SSSs"; Pattern pattern = Pattern.compile ("^ ([hh][tt]{2}[pp]://|[ hh][tt]{2}[pp][ss]://) (([a-za-z0-9-~]+).) + ([a-za-z0-9-~\\/]) +$ "); System.out.println (Pattern.matcher (URL1). matches ()); System.out.println (Pattern.matcher (URL2). matches ()); System.out.println (Pattern.matcher (URL3). matches ()); System.out.println (Pattern.matcher (URL4). matches ());}}
After you have set up a regular expression with string strings, convert to regular expressions by Pattern.compile.

Then use the Pattern.matcher (XX). Matches () method to get a Boolean value that matches.

Since the regular expression is set by the string type in Java, in which the/do not fill in the front \, but the regular expression of the \ need to fill up a \, that is, \ \ to represent a \, which is different from JavaScript.

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

"Java" uses regular expressions to determine whether URLs are

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.