The mysteries of regular expressions and the mysteries of Regular Expressions

Source: Internet
Author: User

The mysteries of regular expressions and the mysteries of Regular Expressions

1. Introduction to Regular Expressions

(1) What is a regular expression: it is a formula that uses a certain pattern to match a type of string. It is mainly used for character string verification, segmentation, search, and replacement operations.

(2) Regular Expressions: replace certain characters in text or code, and the editing software cannot complete the search and replacement. You can use regular expressions to complete almost all text search and replacement work.

The powerful function of a regular expression is that it can specify the abstract mode to verify many different character sequences. Three basic abstract modes can be set:

1. Applicable character sets (such as letters, numbers, and underscores)

2. Select a set of rows (for example, "com", "net", or "org ")

3. Repeatable sequence (for example, the number of characters less than 8 characters in a short time)

(By combining the three modes in different ways, you can check whether the format of the electronic right-click address entered by the user is correct and whether the date format entered by the user is correct; check whether the format of the ID card number entered by the user is correct)

2. Regular Expression is the basis

(1) metacharacters: Regular Expressions are composed of some common characters and metacharacters. common characters include uppercase and lowercase letters and numbers, while metacharacters have special meanings.

Metacharacters are used to describe the appearance of the character before them in the matching mode. Metacharacters are single characters, but different or identical metacharacters can be combined to form large metacharacters. The table describes the meanings of metacharacters.

Character Description
() Groups different grandfathers, also known as sub-patterns.
[] Define Character Set combination
{} Number of repeated times
^ Matches strings or indicates they are not in a character set.
$ Match at the end of character Transfer
. Match any character other than the linefeed (\ n)
? Repeat zero times or once
* Repeated zero or multiple times
+ Repeat once or multiple times
\ Conversion character
| Or select the branch ID.

The regular expression syntax is case sensitive.

(2) square brackets expression: it can be used to define a character set combination. It means that it must match any character in the set. There are two ways to define square brackets expressions:

1. List all characters in. For example, the square brackets expression "[1234567890]" can match any number character, but can only match one character.

2. Use the "-" character to represent the character range. For example, the square brackets expression "[0123456789 abcdefABCDEF]" can be abbreviated as a square brackets expression "[0-9a-fA-F]".

(In general, the character "-" is only a common character and is used as a metacharacter only when the expression range is used. For example, the first character "-" of the square brackets expression [-a-z] only represents a common character, and the second is a metacharacter.

Used to indicate the range of letters a to z)

Common square brackets:

Character Description
[A-z] Lowercase letter
A-Z Uppercase letter
[A-zA-Z] Uppercase/lowercase letters
[0-9] Numeric characters
[0-9 \ + \-\ * \] Number character, plus sign, minus sign, multiplication number, division number
[\ F \ r \ n \ t] White space characters

(3) Positioning OPERATOR: square brackets can only be used to match one character, such as "Y8", "B3", and "A6. Here we need to use the positioning characters "^" and "$"

The locator is used to describe the boundary of a string or word, including ^ ("shift + 6 keys") and $ (dollar sign) they indicate start matching and end matching of character transfer respectively.

You can use location operators and square brackets to create regular expressions with different functions. For example, "^ [A-Z] [0-9] $" can be used to verify a string consisting of two characters, and the string must satisfy both

Conditions that start with an uppercase or lowercase letter and end with a number.

If the metacharacter "^" appears in the square brackets expression, it indicates whether the meaning is "no". If it appears outside the square brackets, it indicates the start of the string.

(4) quantifiers: a qualifier used to specify the number of times a character must appear. That is to say, the quantifiers must be used when the recurrence mode is specified. Quantifiers include *, +, and ,? , {N}, {n ,}, {n, m} are used in six ways, as shown in the table:

Character Description
* Repeat any time (including zero times)
+ Repeat once or multiple times
? Zero or one repetition
{N} Repeat n times, where n is a non-negative number
{N ,} Repeat at least n times, where n is not negative
{N, m} Repeat at least n to m times, where m and n are both non-negative and m is greater than or equal to n

You can use quantifiers to specify the number of times a regular expression is found in a search. For example, the regular expression "Limit {1, 2}" can be used to verify any string "Limit" and "sungsung"

Quantizer "?" If it appears in square brackets, it also represents a common character. For example, "[-0-9?]" Can be used to verify any number or a common character "? "Or a common character"-".

(5) Selection character: the vertical line "|" in the regular expression can indicate selecting a branch. For example, the regular expression "aaa | bbb | ccc" can be used to verify the string "aaa", string "bbb", or "ccc ". You can use the combination of selection and other metacharacters to complete more complex regular expressions, such: the regular expression "^ [0-9] | [a-z]" can be used to verify the start of a number or letter.

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.