Understanding Regular Expressions
If you have not used regular expressions before, you may not be familiar with this term or concept. However, they are not as novel as you might think.
Recall how you found the file on your hard disk. Are you sure you will use it? and * characters to help find the file you are looking for. The character matches a single character in the file name, while the * matches one or more characters. One like ' data?. DAT ' mode can be found in the following files:
Data1.dat
Data2.dat
Datax.dat
DataN.dat
If you use the * character instead? Character, the number of files found will be enlarged. ' Data*.dat ' can match all of the following file names:
Data.dat
Data1.dat
Data2.dat
Data12.dat
Datax.dat
DataXYZ.dat
Although this method of searching for files is certainly useful, it is very limited. and * The limited ability of wildcards allows you to have a concept of what regular expressions can do, but regular expressions are more powerful and flexible.
The early origins of regular expressions
The "ancestors" of regular expressions can be traced back to early studies of how the human nervous system works. Warren McCulloch and Walter Pitts, two neuroscientists, have developed a mathematical way of describing these neural networks.
In 1956, an American mathematician named Stephen Kleene, based on the early work of McCulloch and Pitts, published a paper entitled "Representation of neural network events", introducing the concept of regular expressions. A regular expression is an expression that describes what he calls the algebra of a regular set, so the term "regular expression" is used.
Subsequently, it was found that this work could be applied to some early studies using Ken Thompson's computational Search algorithm, and Ken Thompson was the main inventor of Unix. The first practical application of regular expressions is the QED editor in Unix.
As they say, the rest is a well-known history. From then until now regular expressions are an important part of text-based editors and search tools.
Using regular expressions
In a typical search and replace operation, you must provide the exact text you want to find. This technique may be sufficient for simple search and replace tasks in static text, but because of its lack of flexibility, it is difficult or even impossible to search for dynamic text.
Using regular expressions, you can:
Tests a pattern of a string. For example, you can test an input string to see if there is a phone number pattern or a credit card number pattern in the string. This is known as data validation.
Replaces text. You can use a regular expression in your document to identify specific text, and then you can delete it all, or replace it with another text.
Extracts a substring from a string based on pattern matching. Can be used to find specific text in text or input fields.
For example, if you need to search the entire Web site to remove some outdated material and replace some HTML formatting tags, you can use regular expressions to test each file to see if there are any material or HTML formatting tags that you want to find in the file. With this method, you can narrow the affected file range to those files that contain the material you want to delete or change. You can then use regular expressions to delete obsolete materials, and finally, you can use regular expressions again to find and replace those that need to be replaced.