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