Tutorials on regular expressions that everyone can understand

Source: Internet
Author: User
The most popular and simple way to write verification rules is regular expressions, but the only problem is that the syntax of regular expressions is too obscure, which makes it very painful. Many developers often use a small copy to remember the complex regular syntax and various common commands to apply complex verification in projects. In this article, I will try to help you understand what a regular expression is and how to learn it more easily.

The most popular and simple way to write verification rules is regular expressions, but the only problem is that the syntax of regular expressions is too obscure, which makes it very painful. Many developers often use a small copy to remember the complex regular syntax and various common commands to apply complex verification in projects.

In this article, I will try to help you understand what a regular expression is and how to learn it more easily.

Maybe you are a beginner. just in case, let's talk about what a regular expression is:

Regular expressions can help us better describe complex text formats. Once you clearly describe these formats, you can use them to retrieve, replace, extract, and modify text data.

The following is a simple example of a regular expression. The first step is to introduce the regular-expression namespace:

using System.Text.RegularExpressions;

The second step is to use the specified regular expression to construct a regular expression object. the following regular expression is used to search for letters a-z with a length of 10:

Regex obj = new Regex("[a-z]{10}");

Finally, match items are retrieved in the specified data according to the regular expression. if the IsMatch method is matched, true is returned.

MessageBox.Show(obj.IsMatch(“shivkoirala”).ToString());
Three important regular expression commands

The best way to remember the regular expression syntax is to remember the three things: Bracket, caret, and Dollars ).

B

There are three types of parentheses in the regular expression.

Square brackets "[" and curly brackets "{".

Square brackets "[" are the characters to be matched, and braces "{" are the specified number of matching characters.

Parentheses "(" is used for grouping.

C Insert the symbol"^"Indicates the start of the regular expression.
D Dollar Sign"$The end of the regular expression.

Now that you know the above three syntaxes, you can write any verification rule in the world. For example, the following example shows how the above three regular expressions are coordinated.

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

  • The above regular expression can only match English letters of a-z, and the matching range is also indicated in brackets.
  • Braces indicate the minimum length and maximum length of the matching string.
  • Finally, to make the expression more rule, the insert symbol "^" and the dollar symbol "$" are added at the beginning and end respectively ".

Now, let's use the above three syntaxes to implement some regular expression validation rules.

Check if the user has entered shivkoirala?

shivkoirala

Let's start the first verification. is the input character between a-g?

[a-g]

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

[a-g]{3}

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

[a-g]{1,3}

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

^[0-9]{8}$

How do I verify a number with a minimum length of 3 and a maximum length of 7, for example, 123,127 4667, 87654?

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

How do I verify the invoice number like LJI1020, with the first three digits of the 8-digit length remaining in the letter?

The first three are letters:

^[a-z]{3}

The following is an 8-digit number:

[0-9]{8}

Therefore, the entire expression is:

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

Verify that the first three digits, such as inv0000203 or inv820830, are case-insensitive English letters, and the remaining eight digits are numbers.

In the previous expression, only the first three invoice numbers with lowercase English letters can be matched. if we enter uppercase letters, the numbers cannot match. Therefore, to ensure that the first three letters are case-insensitive, we need to use the expression ^ [a-zA-Z] {3 }.

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

Can we verify the simple URL format?

Step 1: Check whether www exists:

^www.

Step 2: The domain name must be 1-15 English letters:

. [a-z]{1,15}

Step 3: end with. com or. org:

. (com|org)$

The complete expression is as follows:

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

Let's take a look at how BCD (which is actually the three basic syntaxes mentioned above) verifies the email format.

Step 1: email starts with an English letter ranging from 1 to 10, followed by "@":

^[a-zA-Z0-9]{1,10}@

Step 2: @ followed by 1-10 English letters, followed by ".":

[a-zA-Z]{1,10}.

Step 3: end with. com or. org:

.(com|org)$

The complete expression is as follows:

^[a-zA-Z0-9]{1,10}@[a-zA-Z]{1,10}.(com|org)$

Verification Value 0-25:

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

The verification format is MM/DD/YYYY, YYYY/MM/DD and DD/MM/YYYY:

Procedure

Regular expression

Description

Check DD first. the length of DD is 1-29 (January), 1-30 (small month), and 1-31 (large month ).

So DD is 1-9 or 01-09.

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

Allow users to enter 1-9 or 01-09.

Add matching 10-19 for DD again

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

Allow users to enter 01-19.

Add matching 20-29 for DD again

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

Allow users to enter 01-29.

I add a match for DD 30-31

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

Finally, you can enter 01-31.

Then match the delimiter "/","-"

[/.-]

You can enter a date separator.

MM is also a similar operation

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

Ask the user to enter the month value 01-12.

The last step is the YY operation.

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

Allow users to enter year 1900-2099.

The regular expression of the date in DD/MM/YYYY format is as follows:

^([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])$

MM/DD/YYYY format date:

^([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])$
Quick Command

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

Actual command Quick Command
[0-9] D
[A-z] [0-9] [_] W
Zero or multiple occurrences *
At least once +
0 or 1 occurrence ?

The full text is complete.

This article is available at http://www.nowamagic.net/librarys/veda/detail/792.

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.