Regular Expressions are widely used in various fields and programming languages related to UNIX/Linux. Linux administrators should be familiar with these expressions. From common shell commands to the famous Perl language to the popular PHP language, it plays an important role. Even the windows command line console supports regular expressions.
It can be said that it is a required knowledge point to learn Linux/UNIX well, otherwise you do not even understand the Linux Startup Script. However, it is indeed a bit obscure, and most of the relevant information is in English, which makes it more difficult to learn. Even if some Chinese translation materials are used, different translators may translate a wide variety of terms. To this end, I decided to write a concise tutorial for it and try to cover the main concepts involved in regular expressions.
I don't want to write this article as a detailed regular expression syntax manual. In fact, these manuals already exist, but it is hard to read them. I hope that after completing this tutorial, you can easily read the regular expression syntax manual of various tools and get started quickly. However, you must make good use of regular expressions, it is not a short tutorial that can solve the problem. It is the result of countless practical exercises. However, the last part of this article puts forward some principled suggestions for writing regular expressions. Learning the experiences of applying these regular expressions to the pioneers will reduce our detours in future practices.
The regular expression is a translation of the English "regular expressions". It is said that it can be traced back to more advanced theories such as "Neural Networks. So what is a regular expression?
A regular expression is a set of regular expressions that match the target string from left to right. Most characters represent themselves in the mode and match the corresponding characters in the target. For The simplest example, The pattern "The quick brown fox" matches a part of The target string that is exactly The same as that of The string.
As mentioned above, regular expressions are used by many tools rooted in UNIX/Linux, but the regular expression syntax of these tools is not exactly the same, some of these extensions of the regular expression syntax are not recognized by other tools, which makes it more difficult to use the regular expression. Therefore, when you use a regular expression in a specific environment, you must first look at the syntax range supported by the target environment to ensure that your regular expression is correctly parsed.
In the examples listed in this article, we use the forward slash "/" as the delimiter of the pattern. A pattern is represented in the following format:
/[A-Z] + (abc | xyz) */I
This article will elaborate on the following regular expression concepts: Pattern modifier, Meta-characters, subpatterns) and reverse reference Back references), Repetition) and quantizer quantifiers), Assertions), comments, recursion in regular expressions. Finally, I will introduce a tool for learning Regular Expressions and some ideas for writing regular expressions.
Regular Expression Pattern modifier (modifier)
The pattern modifier of a regular expression is used to specify the matching method between the pattern and the target string. For example, whether a case-sensitive match is required, whether the pattern is single-line or multi-line. The space and line feed in the modifier are ignored. Other characters may cause errors. The following lists some common pattern modifiers. Note that the pattern modifier is case sensitive.
I: Non-case sensitive mode. If this modifier is set, characters in the mode match both uppercase and lowercase letters.
M: multiline mode. If this modifier is set, the start and end of the line must match the start and end of the entire string, it also matches the following and the previous line breaks respectively.
S: single-line mode. If this modifier is set, the dot metacharacters (.) In the pattern match all characters, including line breaks. If this parameter is not set, line breaks are not included.
For the multi-row mode and single-row mode, it is easy for beginners to confuse the two because they are not literally mutually exclusive. In fact, they only define English periods (.), the tone character (^) and the dollar sign ($) match the three metacharacters. Therefore, the single-line mode and multi-line mode modifier can be used at the same time.
X: If this modifier is set, the white space characters in the mode are ignored except for escaped characters or in the character class, all the characters between the # And the next line break except the unescaped character class, including both ends, are ignored. It allows you to add comments in complex modes. We will explain in detail the comments in regular expression in the following sections.
There are still many pattern modifiers, which are not listed here. We will introduce some other pattern modifiers in combination with the following content. You can also add your own pattern modifiers for different tools, but these are the most common.
The pattern modifier usually follows the pattern definition Terminator, for example, the final "I" character of the pattern in the following example. /[A-Z] + (abc | xyz) */I, this modifier takes effect for the entire match mode. The pattern modifier can also be included in the pattern "(? "And. For example ,(? Im) is set to case-insensitive, multi-line mode. You can also remove these options by adding a minus sign in front of the letter. For example, the combined options (? Im-s), set case-insensitive and Multiline mode, and cancel the single line mode. If a letter appears before and after the minus sign, this option is disabled.
NOTE: If (? The appearance of im-s in a submode (included by another pair of parentheses) limits the pattern modifier to this submode.
- Linux init process and related files
- Detailed analysis of ten aspects of Linux Server Security Protection
- Share a simple Linux data backup solution
- How to handle Linux crashes
- Detailed introduction to Linux hardware information commands