Microsoft's regular expression tutorial (1): Regular Expression Introduction

Source: Internet
Author: User

Recognize Regular Expressions

If you have never used a regular expression, you may not be familiar with this term or concept. However, they are not as novel as you think.

Remember how to find files on the hard disk. Are you sure you want to use? And * characters to help find the file you are looking .? Character matches a single character in the file name, while * matches one or more characters. A file such as 'data ?. The following files can be found in the DAT mode:

Data1.dat

Data2.dat

Datax. dat

Datan. dat

If "*" is used instead? Characters to expand the number of files found. 'Data *. dat 'can match all the following file names:

Data. dat

Data1.dat

Data2.dat

Data12.dat

Datax. dat

Dataxyz. dat

Although this file search method is certainly very useful, it is also very limited .? The limited capabilities of wildcard and * enable you to define what a regular expression can do. However, regular expressions are more powerful and flexible.

Early origins of Regular Expressions

The "Ancestor" of regular expressions can be traced back to early studies on how the human nervous system works. Warren McCulloch and Walter Pitts, two neuroscientists, developed a mathematical method to describe these neural networks.

In 1956, an American mathematician named Stephen Kleene published a paper titled "neural network event representation" based on McCulloch and Pitts's early work, introduces the concept of regular expressions. A regular expression is an expression used to describe the algebra of a positive set. Therefore, the regular expression is used.

Later, we found that this work can be applied to the computing search using Ken Thompson.AlgorithmIn some early studies, Ken Thompson was the principal inventor of UNIX. The first practical application of Regular ExpressionsProgramIs in UNIXQEDEditor.

As they said, the rest is the well-known history. Since then, regular expressions have been an important part of text-based editors and search tools.

Use Regular Expressions

In typical search and replacement operations, you must provide the exact text to be searched. This technology may be sufficient for simple search and replacement tasks in static text, but it is difficult or even impossible to search dynamic text due to its lack of flexibility.

With a regular expression, you can:

    • Test a mode of a string. For example, you can test an input string to see if there is a phone number or a credit card number. This is called Data Validity verification.
    • Replace text. You can use a regular expression in a document to identify a specific text, and then delete it all or replace it with another text.
    • Extract a substring from the string based on the pattern match. It can be used to search for specific text in text or input fields.

for example, if you need to search the entire web site to delete outdated materials and replace some HTML formatting tags, you can use regular expressions to test each file, check whether there are materials or HTML formatting tags in the file. With this method, you can narrow down the affected files to the files that contain the materials to be deleted or changed. Then, you can use a regular expression to delete outdated materials. Finally, you can use a regular expression to find and replace the tags that need to be replaced.

Related Article

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.