----Regular expressions in the Java language

Source: Internet
Author: User

DAY14----Regular Expressions in the Java language

First, overview:

Regular expressions In the Java language is also considered a more important module, before we learned some of the basis for regular expressions, in the login registration function is widely used, so I do not do the redundant introduction here. Let's take a look at some examples of regular expressions,


Second, the regular expression:

A regular expression is actually a defined string that expresses a rule and pattern. It is primarily used to test our users ' input as required during the experience. is primarily based on a class of Java to implement validation. Let's take a look at the regular expression in the following example

1. Pattern class:

He is a pattern class, a class that compiles a regular expression. By creating this class to implement the rules of an expression,

Note: This class is not able to create objects through new, but is created by invoking one of his own compile methods.

2, Matcher class:

He is a matching class that implements validation of expressions. This means that the class that encapsulates a string that needs to be validated can be validated against the rules we define. If you are validating the registration, you need to get the string in the input box to match the validation.

3. Example: Define a regular expression that matches a phone number.

Package www.com.c1;//Guide Pack import Java.util.regex.matcher;import Java.util.regex.pattern;public class Reg01 {public static void Main (string[] args) {///Use the exposed static method in the background to create the object without using the New keyword to create it. Specifies the format of a phone number, calling the static compile method. Pattern p1 =pattern.compile ("1[3578][0-9]{9}");//Specify a string you need to validate, call the non-static Matcher method Matcher m = P1.matcher ("15761597440") ;//Determine if the match, call his own matches method to do the judgment if (M.matches ()) {System.out.println ("The phone number is valid! ");} else {System.out.println ("The phone number is invalid! ");} Other commonly used methods are the//split () method, which separates the strings according to the specified delimiter ":". Returns a string array of type pattern P2 = pattern.compile (":"); String str[] = P2.split ("01:02:03:04:05"); for (int i = 0; i<str.length;i++) {System.out.println (str[i]);}}}


Third, concluding remarks:

Here we are exposed to the definition and use of some regular expressions. Other ways to use regular expression classes can be learned from other channels. Here is a preliminary discussion.


This article from the "Program Ape" blog, reproduced please contact the author!

----Regular expressions in the Java language

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.