Article title: unveil the mysteries of regular expression syntax. Linux is a technology channel of the IT lab in China. Includes basic categories such as desktop applications, Linux system management, kernel research, embedded systems, and open source.
Regular expressions (REs) are generally incorrectly considered as a mysterious language that only a few people understand. On the surface, they do look messy. if you don't know its syntax, its code is just a pile of text spam in your eyes. In fact, regular expressions are very simple and understandable. After reading this article, you will be familiar with the general syntax of regular expressions.
Supports multiple platforms
The regular expression was first proposed by the mathematician Stephen Kleene in 1956, based on his incremental research on natural language. Regular expressions with complete syntax are used in character format matching and subsequently applied to the field of melt information technology. Since then, regular expressions have evolved over several periods, and the current standards have been approved and recognized by ISO (International Standards Organization.
Regular expressions are not a specialized language, but can be used to search for and replace text in a file or character. It has two standards: basic regular expressions (BRE) and extended regular expressions (ERE ). ERE includes BRE functions and other concepts.
Many programs use regular expressions, including xsh, egrep, sed, vi, and programs on UNIX platforms. They can be adopted by many languages, such as HTML and XML, which are usually only a subset of the entire Standard.
It's more common than you think.
As regular expressions are transplanted to cross-platform programming languages, this function is becoming increasingly complete and widely used. The search engine on the network uses it, and the e-mail program uses it, even if you are not a UNIX programmer, you can also use the rule language to simplify your program and shorten your development time.
Regular expressions 101
The syntax of many regular expressions looks very similar, because you have never studied them before. A wildcard is a structure type of RE, that is, repeated operations. Let's take a look at the most common basic syntax types of the ERE standard. To provide examples for specific purposes, I will use several different programs.
Character matching
The key to a regular expression is to determine what you want to search for. without this concept, Res will be useless.
Each expression contains the command to be searched, as shown in Table.
Repeated operators
Repeated operators, or quantifiers, all describe the number of times a specific character is searched. They are often used in character matching syntax to find characters with multiple rows. See Table B.
The anchor is the matching format, as shown in C. It allows you to conveniently find the merge of common characters. For example, I use the vi line editor command: s to represent substitute. the basic syntax of this command is:
S/pattern_to_match/pattern_to_substitute/
Interval
Another item in Res is the delimiter (or insert. In fact, this symbol is equivalent to an OR statement and represents a | symbol. The following sentence returns the "nerd" and "merd" handles in the sample.txt file:
Egrep "(n | m) erd" sample.txt
The interval function is very powerful, especially when you are looking for different spelling of files, but you can get the same result in the following example:
Egrep "[nm] erd" sample.txt
When you use the interval function to connect with the advanced features of Res, its real use is more obvious.
Reserved characters
The last and most important feature of Res is to retain characters (also known as specific characters ). For example, if you want to find the characters "ne * rd" and "ni * rd, the format match statement "n [ei] * rd" matches "neeeeerd" and "nieieierd", but it is not the character you want to search. Because '*' (asterisk) is a reserved character, you must use a backslash to replace it, that is, "n [ei] \ * rd ". Other reserved characters include:
^ (Carat)
. (Period)
[(Left bracket}
$ (Dollar sign)
(Left parenthesis)
) (Right parenthesis)
| (Pipe)
* (Asterisk)
+ (Plus symbol)
? (Question mark)
{(Left curly bracket, or left brace)
\ Backslash
Once you include the above characters in your character search, there is no doubt that Res becomes very difficult to read. For example, the eregi search engine code in the following PHP is hard to read.
As you can see, the program's intentions are hard to grasp. However, if you leave the reserved characters aside, you often mistakenly understand the meaning of the code.
Summary
In this article, we unveil the mystery of regular expressions and list the general syntax of the ERE standard. If you want to view the complete description of the Open Group rules, see Regular Expressions. You are welcome to post your questions or opinions in the discussion area.
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