Regular expression tutorials that everyone can understand

Source: Internet
Author: User

b There are 3 types of brackets in the regular expression "[" and Braces "{". The square brackets "[" are the characters that need to be matched, and the curly braces "{" are the number of matching characters specified. The parentheses "(" are used for grouping.) The C caret "^" indicates the beginning of the regular type. The D dollar sign "$" indicates the end of the regular type. 

Now that you know the 3 syntax above, you can write any validation rule in the world. For example, the following example is a good illustration of how the above 3 regular grammars work in a coordinated way.

Note: There is an error, "()" should be "{}"

Check if the user has entered Shivkoirala?

Shivkoirala

Let's start with the first validation, the characters entered between a-g?

[A-g]

The characters entered are between a-g and the length is 3?

[A-g] {3}

The characters entered are between a-g and the maximum length is 3 minimum length of 1?

[A-g] {1,3}

How do I match a fixed 8-digit number like 91230456, 01237648?

^[0-9]{8}$

How can I verify that the minimum length is 3 with a maximum length of 7, such as: 123, 1274667, 87654?

^[0-9]{3,7}$

How to verify the invoice number like LJI1020, the first 3 is the number of letters remaining 8 digits long?

The first three are the letters:

^[A-Z]{3}

The following is a 8-bit length number:

[0-9] {8}

So the whole expression is:

^[a-z]{3}[0-9]{7}$

Verify that the first 3 bits, such as INV190203 or inv820830, are case-insensitive and the remaining 8 digits are numbers.

In the previous expression, only the first 3 are the lowercase English letters of the invoice number, if we enter the capital letter that will not match. So to make sure the first 3 letters are case-insensitive, we're going to use an expression ^[a-za-z]{3}.

^[a-za-z]{3}[0-9]{7}$

Can we verify the simple URL URL format?

First step: Check if there is www:

^www.

The second step: the domain name must be a length of 1-15 in the English alphabet:

. [A-z] {1,15}

Step three: End With. com or. org:

. (com|org) $

The complete expression is as follows:

^www[.] [A-z] {1,15} [.] (com|org) $

Let's look at how BCD (in fact, the 3 basic syntax above) validates the email format.

The first step: email starts with an English letter of 1-10 in length, followed by an "@":

^[a-za-z0-9]{1,10}@

The second step: at the back of the @ is the English alphabet with a length of 1-10, followed by a "." :

[A-za-z] {1,10}.

Step three: End With. com or. org:

. (com|org) $

The final complete expression is as follows:

^[a-za-z0-9]{1,10}@[a-za-z]{1,10}. (com|org) $

Verify that the value is in the 0-25 number:

^ ([0-9]) | ([0-1][0-9]) | ([0-2][0-5])) $

Verify that the format is mm/dd/yyyy, YYYY/MM/DD, and dd/mm/yyyy date:

Step

 

Regular

 

Description description

 

First check DD. First DD is 1-29 long ( February), 1-30 (month-small), 1-31 (month-old).

 

So DD is 1-9 or 01-09

 

[1-9]|0[1-9]

 

The

Allows the user to enter 1-9 or 01-09.

 

Add matching 10-19 to DD

 

[1-9]|1[0-9]

 

The

Allows the user to enter 01-19.

 

Add matching 20-29 to DD

 

[1-9]|1[0-9]|2[0-9]

 

The

Allows the user to enter 01-29.

 

I then add a match 30-31 to DD

 

[1-9]|1[0-9]|2[0-9]|3[0-1]

 

The last user can enter 01-31.

 

To match the delimiter for the day period "/", "-"

 

[/.-]

 

The

Allows the user to enter a date separator.

 

mm is a similar operation

 

[1-9]|0[1-9]|1[0-2]

 

The

lets the user enter a month value of 01-12.

 

Last is the action of YY

 

1[9][0-9][0-9]|2[0][0-9][0-9]

 

The

Allows the user to enter the year 1900-2099.

The regular expression for the date in the last dd/mm/yyyy format is:

^ ([1-9]|0[1-9]|1[0-9]|2[0-9]|3[0-1]) [-/.] ([1-9]|0[1-9]|1[0-2]) [- / .] (1[9][0-9][0-9]|2[0][0-9][0-9]) $

Date in MM/DD/YYYY format:

^ ([1-9]|0[1-9]|1[0-2]) [-/.] ([1-9]|0[1-9]|1[0-9]|2[0-9]|3[0-1]) [- / .] (1[9][0-9][0-9]|2[0][0-9][0-9]) $

Date in YYYY/MM/DD format:

^ (1[9][0-9][0-9]|2[0][0-9][0-9]) [-/.] ([1-9]|0[1-9]|1[0-2]) [- / .] ([1-9]|0[1-9]|1[0-9]|2[0-9]|3[0-1]) $

Shortcut commands

You can also use the following shortcut commands to simplify your regular expression:

Actual command

Shortcut commands

[0-9]

D

[A-z] [0-9] [_]

W

Occurs 0 or more times

*

At least one time.

+

Occurs 0 or 1 times

?

Complete the full text.

Regular expression tutorials that everyone can understand

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.