Java basics: Regular Expressions

Source: Internet
Author: User

Exercise 1: Write a method to determine whether a given string is a combination of full numbers

Exercise 2: compile a method to determine whether a given string is a phone number

Package day7; import java. util. regex. matcher; import java. util. regex. pattern;/*** java. util. the regex package * Pattern class is mainly used to compile regular expression specifications; * The Matcher class is mainly used to execute the specification and verify whether the string complies with its specifications; */public class RegexDemo {public static void main (String [] args) {String str = "1234"; String str1 = "a234"; System. out. println (isDigitCom (str); // trueSystem. out. println (isDigitCom (str1); // falseString str2 = "025-12345678"; System. out. println (isPhon ENumCom (str2 )); // true}/*** determines whether the given string is a combination of all numbers * @ param str the string to be judged * @ return returns true if it is a combination of all numbers, otherwise, false */public static Boolean isDigitCom (String str) {// 1. write specification String regex = "[0-9] +"; // regex = "\ d +"; // 2. compile the given regular expression to Pattern p = Pattern. compile (regex); // 3. create a Matcher m = p. matcher (str); // 4. try to match the entire region with the pattern. Boolean B = m. matches (); return B;}/*** determines whether the given string is a telephone number: 025-12345678, 0515-1234567 * @ param str * @ return */public static Boolean isPhoneNumCom (String str) {String regex = "[0] {1} \ d {2}-[1-9] {1} \ d {7} | [0] {1 }\ \ d {3}-[1-9] {1} \ d {6 }"; pattern p = Pattern. compile (regex); Matcher m = p. matcher (str); Boolean B = m. matches (); return B ;}}

 

The String class has three methods that support regular expressions:

 

Boolean matches (String regex) indicates whether the String matches the given regular expression.

String replaceAll (String regex, String replacement) replaces all the substrings matching the given regular expression with the given replacement.

String [] split (String regex) Splits a String based on a given regular expression.

 

The matching process can be simplified as follows:

Public class RegexDemo2 {public static void main (String [] args) {// determines whether a String is a full-digit combination String regex = "[0-9] + "; string str= "12324"; String str1 = "a234"; System. out. println (str. matches (regex); // trueSystem. out. println (str1.matches (regex )); // false // determine whether a String is a telephone number String regex2 = "[0] {1} \ d {2}-[1-9] {1} \ d {7} | [0] {1} \ d {3}-[1-9] {1} \ d {6 }"; string str2 = "025-12345678"; System. out. println (str2.matches (regex2); // true }}

The Pattern class defines the regular expression specification. Common

Specification 1:

[Abc] indicates characters a, B, or c.

[^ Abc] any character except a, B, or c

[0-9] indicates any number between 0-9.

[A-zA-Z0-9] represents letters or numbers

\ D indicates any number

 

Specification 2: (X indicates a set of specifications)

X must appear once

X? 0 or 1 occurrence

X * appears 0 or more times

X + appears once or more

X {n} must appear n times

X {n,} must appear more than n times

X {n, m} appears n to m times

 

Specification 3: (X and Y indicate a set of specifications)

XY indicates that X is followed by Y.

X | Y indicates the X specification or Y specification.

(X) indicates a capture group specification.

 

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.