Understand regular expressions
If you have never used regular expressions, you may be less familiar with this term and concept. They aren't as novel as you think, though.
Please recall how to find files on your hard drive. You will definitely use the ? and * characters to help find the file you are looking for. ? Characters match a single character in the file name, while * matches one or more characters. A pattern like 'data?.dat' can find the following file:
data1.dat
data2.dat
datax.dat
dataN.dat
If the * character is used instead of the ? character, the number of files found will be expanded. 'data*.dat' can match all file names below:
data.dat
data1.dat
data2.dat
data12.dat
datax.dat
dataXYZ.dat
Although this method of searching files is certainly useful, it is also very limited. The limited ability of ? and * wildcards can give you a concept of what regular expressions can do, but regular expressions are more powerful and flexible.
Early Origins of Regular Expressions
The ancestors of regular expressions can be traced back to early studies on how the human nervous system works. Warren McCulloch and Walter Pitts, two neurophysiologists, have developed a mathematical way to describe these neural networks.
In 1956, an American mathematician named Stephen Kleene published a paper titled Notation of Neural Net Events based on the early work of McCulloch and Pitts, introducing the concept of regular expressions. Regular expressions are expressions used to describe algebras that it calls regular sets, so the term regular expression is used.
It was subsequently discovered that this work could be applied to some early research using Ken Thompson's computational search algorithm, the main inventor of Unix. The first practical application for regular expressions is the qed editor in Unix.
As they said, what remains is well-known history. Regular expressions have been an important part of text-based editors and search tools since then.
Using regular expressions
In typical search and replacement operations, the exact text to be found must be provided. This technique may be sufficient for simple search and replacement tasks in static text, but due to 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 called data validation.
Replace text. You can use a regular expression in a document to identify a specific text, and then you can delete it all, or replace it with another text.
Extract a substring from the string according to 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 test each file using regular expressions to see if the material or HTML you are looking for exists in that file Format the marker. This method allows you to narrow the affected files to those files containing the material you want to delete or change. Then you can use regular expressions to remove outdated materials, and finally, you can use regular expressions again to find and replace those tags that need to be replaced.