Java Basics-Regular expressions

Source: Internet
Author: User
Regular expressions

The regular expression defines the pattern of the string.
Regular expressions can be used to search, edit, or manipulate text.
Regular expressions are not limited to a single language, but have subtle differences in each language.
Grammar:
In Java, \ means: I want to insert a backslash for a regular expression, so the character after it has a special meaning.

String pattern = "(\\D*)(\\d+)(.*)";

Methods of the Matcher class
Index method
The index method provides useful index values that exactly indicate where the match is found in the input string:

Research methods
The research method is used to check the input string and return a Boolean value indicating whether the pattern is found:

Replacement method
The replacement method is the method of replacing the text in the input string:

Example:

public class regex {public static void main(String[] args) {checkQQ2("0123134");}public static void checkQQ2(String qq) {String reg = "[1-9][0-9]{4,14}";System.out.println(qq.matches(reg)?"合法qq":"非法qq");}}

Java Basics-Regular expressions

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.