Linux basics tutorial ---- Regular Expression Basics
Introduction
Although the title of this section is a regular expression, this section only introducesgrep,sed,awkThese three commands, while the regular expression can be used as one of the three commands (the command output can contain the regular expression ). The regular expression itself has a lot of content. It needs to be explained clearly by a separate course. However, the relevant content in this section can also meet the requirements in many situations.
I. Regular Expressions
What is a regular expression?
Regular Expression, also known as Regular Expression, rule Expression, Regular Expression (English: Regular Expression, often abbreviated as regex, regexp or RE in code ), A concept of computer science. Regular Expressions use a single string to describe and match a series of strings that conform to a certain syntax rule. In many text editors, regular expressions are usually used to retrieve and replace texts that match a certain pattern.
Many programming languages Support string operations using regular expressions. For example, a powerful Regular Expression Engine is built in Perl. The concept of regular expressions was initially developed by tool software in UNIX (for examplesedAndgrep. Regular Expressions are abbreviated as "regex". The singular values include regexp and regex, And the plural values include regexps, regexes, and regexen.
In simple terms, regular expressions and functions are very similar to the wildcards mentioned above, but they are quite different, especially in the meanings of some special matching characters, do not confuse the two.
Linux Regular Expression sed details
Linux regular expression features and differences between BRE and ERE
Grep uses concise and Regular Expressions
Regular Expression usage
Assertion with Zero Width of a regular expression
Regular Expressions and file formatting commands in Linux (awk/grep/sed)
Basic Regular Expression
Regular Expressions
1. Example
Suppose we have such a text file that contains the "shiyanlou" and "shilouyan" strings, the same expression:
shi*
If this is used as a regular expression, it can only match shi. If it is not used as a regular expression*As a wildcard, both strings are matched. Why? Because in the regular expression*Matches the previous subexpression (the character before it) zero or multiple times. For example, it can match "sh", "shii", "shish", "shiishi", and so on, as a wildcard, it matches any number of characters after the wildcard, so it can match "shiyanlou" and "shilouyan.
After the experience, let's start learning regular expressions.
2. Basic Syntax:
A regular expression is usually called a pattern. It is used to describe or match a series of strings that conform to a certain syntax rule.
Select
|Vertical separator indicates selection, for example, "boy | girl" can match "boy" or "girl"
Quantity limit
The quantity limit is exclusive to*And+Plus sign,?Question mark,.Point number. If a number qualifier is not added in a mode, the number appears once and only once:
+Indicates that the previous character must appear at least once (once or multiple times). For example, "goo + gle" can match "gooogle" and "goooogle;
?Indicates that the previous character appears at most once (0 or 1), for example, "colou? R ", can match" color "or" color ";
*An asterisk indicates that the preceding character can appear either once or multiple times (0, or 1, or multiple times). For example, "0*42" can match 42, 042, 0042, 00042, and so on.
Range and priority
()Parentheses can be used to define the range and priority of the pattern string, which can be simply understood as whether to take the pattern string in the brackets as a whole. For example, "gr (a | e) y" is equivalent to "gray | gray" (priority is defined here, and vertical delimiters are used to select a or e instead of gra And ey ), "(grand )? Father matches father and grandfather,?Use the content of the parentheses as a complete match ).
Syntax (Part)
Regular expressions have a variety of different styles. The following lists some of the commonly usedperlAndpythonProgramming Language andgrepOregrepRegular Expression matching rules: (due to the markdown table parsing problem, the vertical separator below is replaced by full-width characters. Please switch back to half-width characters in actual use)
PCRE (Perl Compatible Regular Expressions) is a Regular expression library written in C and compiled by Philip Hazel. PCRE is a lightweight function library, which is much smaller than a regular expression library such as Boost. PCRE is very easy to use and has powerful functions. Its performance exceeds the POSIX Regular Expression Library and some classic Regular Expression Libraries.
| Character |
Description |
| \ |
Mark the next character as a special character or a literal character. For example, "n" matches the character "n ". "\ N" matches a line break. The sequence "\" matches "\", and "\ (" matches "(". |
| ^ |
Matches the start position of the input string. |
| $ |
Matches the end position of the input string. |
| {N} |
N is a non-negative integer. Match n times. For example, "o {2}" cannot match "o" in "Bob", but can match two o in "food. |
| {N ,} |
N is a non-negative integer. Match at least n times. For example, "o {2,}" cannot match "o" in "Bob", but can match all o in "foooood. "O {1,}" is equivalent to "o + ". "O {0,}" is equivalent to "o *". |
| {N, m} |
Both m and n are non-negative integers, where n <= m. Match at least n times and at most m times. For example, "o {1, 3}" matches the first three o in "fooooood. "O {0, 1}" is equivalent to "o ?". Note that there must be no space between a comma and two numbers. |
| * |
Matches the previous subexpression zero or multiple times. For example, zo * can match "z", "zo", and "zoo ". * Is equivalent to {0 ,}. |
| + |
Match the previous subexpression once or multiple times. For example, "zo +" can match "zo" and "zoo", but cannot match "z ". + Is equivalent to {1 ,}. |
| ? |
Match the previous subexpression zero or once. For example, "do (es )?" It can match "do" in "do" or "does ".? It is equivalent to {0, 1 }. |
| ? |
When this character is followed by any other delimiter (*, + ,?, The matching mode after {n}, {n ,}, {n, m}) is not greedy. The non-Greedy mode matches as few searched strings as possible, while the default greedy mode matches as many searched strings as possible. For example, for strings "oooo", "o + ?" A single "o" will be matched, while "o +" will match all "o ". |
| . |
Match any single character except "\ n. To match any character including "\ n", use a pattern like "(. | \ n. |
| (Pattern) |
Match pattern and obtain the matched substring. This substring is used for backward reference. To match the parentheses, use "\ (" or "\)". |
| X | y |
Match x or y. For example, "z | food" can match "z" or "food ". "(Z | f) ood" matches "zood" or "food ". |
| [Xyz] |
Character set (character class ). Match any character in it. For example, "[abc]" can match "a" in "plain ". Special characters only have the special meaning of backslash \, which is used to escape characters. Other special characters, such as asterisks, plus signs, and brackets, are common characters. Escape Character ^ if it appears in the first place, it indicates the combination of negative character sets. If it appears in the middle of the string, it is only a common character. Hyphen-If it appears in the middle of the string, it indicates the description of the character range. If it appears in the first place, it is only a common character. |
| [^ Xyz] |
Exclude character set (negate. Match any character not listed. For example, "[^ abc]" can match "plin" in "plain ". |
| [A-z] |
Character range. Matches any character in the specified range. For example, "[a-z]" can match any lowercase letter in the range of "a" to "z. |
| [^ A-z] |
The excluded character range. Matches any character that is not within the specified range. For example, "[^ a-z]" can match any character that is not in the range of "a" to "z. |
Priority
The priority is reduced from top to bottom to left:
| Operator |
Description |
| \ |
Escape Character |
| (),(? :),(? =), [] |
Brackets and brackets |
| *, + ,? , {N}, {n ,}, {n, m} |
Qualifier |
| ^, $, \ Any metacharacters |
Location and Sequence |
| | |
Select |
Mind Map of regex:
For more details, please continue to read the highlights on the next page: