Regular Expression,

Source: Internet
Author: User
Tags character classes

Regular Expression,
Chapter 2Regular Expression1.1Concepts of Regular Expressions

Regular Expression(English: Regular Expression, often abbreviated as regex in the Code ).

A regular expression is a string that is described by a single string and used to define matching rules and match a series of strings that conform to a certain syntax rule. During development, regular expressions are usually used to retrieve and replace texts that comply with certain rules.

1.2 Regular Expression matching rules

Refer to the help document to define regular expressions in the Pattern class. Regular Expressions are case-sensitive. Let's learn the syntax rules.

Regular expression syntax rules:

Character: x

Meaning: represents the character x

For example, the matching rule is"", The content of the string to be matched is ""

 

Character:\\

Meaning: it indicates the backslash character '\'

For example, the matching rule is"\\",The string content to be matched is "\"

 

Character:\ T

Description: Tab

For example, the matching rule is"\ T", The corresponding effect is to generate a tab Space

 

Character:\ N

Meaning: linefeed

For example, the matching rule is"\ N", The corresponding effect is to wrap the line, the cursor is in the next row of the original position

 

Character:\ R

Meaning: Carriage Return

For example, the matching rule is"\ R", The corresponding effect is the effect after the carriage return, the cursor comes to the beginning of the next line

 

Character class:[Abc]

Meaning: It indicates characters a, B, or c.

For example, the matching rule is"[Abc]", The content to be matched is a character of a, B, or c.

 

Character class:[^ Abc]

Meaning: represents any character except a, B, or c

For example, the matching rule is"[^ Abc]"The content to be matched Is Not a, B, or any character that is not c

 

Character class:[A-zA-Z]

Meaning: It indicates a to z or A to Z. The two letters are included.

For example, the matching rule is"[A-zA-Z]"To match an uppercase or lowercase letter.

 

Character class:[0-9]

Meaning: 0 to 9 digits. The two digits are included.

For example, the matching rule is"[0-9]"To match a number.

 

Character class:A-zA-Z_0-9

Meaning: letters, numbers, or underscores)

For example, the matching rule is"[A-zA-Z_0-9]"To match a letter, a number, or a slide line.

 

Predefined character classes:.

Meaning: represents any character

For example, the matching rule is"."To match any character. If you want to use., use the matching rule "\.".

 

Predefined character classes:\ D

Meaning: 0 to 9 digits. The two digits are included, which is equivalent to [0-9].

For example, the matching rule is"\ D"To match a number.

 

Predefined character classes:\W

Meaning: It indicates a letter, number, or underline (a word character), which is equivalentA-zA-Z_0-9

For example, the matching rule is"\ W", You must match a letter, a number, or a slide line.

 

Boundary:^

Meaning: indicates the beginning of a row.

For example, the matching rule is^ [Abc] [0-9] $, The content to be matched starts from the [abc] position, which is equivalent to the Left Double quotation mark.

 

Boundary:$

Meaning: indicates the end of a row.

For example, the matching rule is^ [Abc] [0-9] $, The content to be matched ends with [0-9], which is equivalent to double quotation marks on the right.

 

Boundary:\ B

Meaning: represents the word boundary.

For example, the matching rule is"\ B [abc] \ B"It indicates the non-word characters (A-zA-Z_0-9)

 

Quantifiers:X?

Meaning: X does not appear once or once.

For example, the matching rule is"? "The matching content is either a character or

 

Quantifiers:X *

Meaning: X appears zero or multiple times

For example, the matching rule is"*", The content to be matched is multiple characters a, or none of

 

Quantifiers:X +

Meaning: X appears once or multiple times

For example, the matching rule is"A +", The content to be matched is multiple characters a, or

 

Quantifiers:X {n}

Meaning: X appears exactly n times

For example, the matching rule is"A {5 }", The content to be matched is five characters

 

Quantifiers:X {n ,}

Meaning: X appears at least n times

For example, the matching rule is"A {5 ,}"The matching content must contain at least five characters.

 

Quantifiers:X {n, m}

Meaning: X appears at least n times, but not more than m times.

For example, the matching rule is"A {5, 8 }"The content to be matched is between 5 characters a and 8 characters.

 

1.3 Regular Expression rule matching exercise

Write a string that meets the following matching rules:

Rule: "[0-9] {6, 12 }"

The rule must match the following content:The length is6 to 12 digits.

For example, if data "123456789" is used for matching, the result is true;

The matching result with data "12345" is false.

 

Rule: "1 [34578] [0-9] {9 }"

The rule must match the following content:11-digit mobile phone number. The fifth digit is 1, and the fifth digit is one of 3, 4, 5, 7, and 8. The last nine digits are any numbers between 0 and 9..

For example, if data "12345678901" is used for matching, the result is false;

The matching result with data "13312345678" is true.

 

Rule: "a * B"

The rule must match the following content:There is a B after multiple a or zero a; B must be the last character.

For example, if "aaaaab" is used for data matching, the result is true;

The matching result with data "abc" is false.

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.