Linux 正则表达式
Attention:
1.linux regular expressions are typically handled in a behavioral unit
2.alias grep= ' grep--color=auto ' to allow matching content to display color
3. Note the character set, export Lcc_all=c
A: What is a regular expression?
Simply put, regular expressions are a set of rules that are defined to handle a large number of strings, for example: Suppose "@" stands for ysg, "! "On behalf of ING. echo "@! "= =" Ysging "
The function is: by defining the assistance of these special symbols, the system administrator can quickly filter, replace or output the required string. Linux regular expressions are typically handled in a behavioral unit.
Second: Why should we learn the regular expression?
In the enterprise work, we do every day in the Linux operations, the moment will face a large number of text configuration with strings, programs, command output and log files, etc., and we often have an urgent need, from a large number of string content to find matching job needs of the specific string, it depends on regular expression. Therefore, it can be said that the regular expression is to filter such a string born.
Three: Easy to confuse two precautions:
A. Regular expressions are widely used in a variety of sounds, such as PHP, Python, Java, and so on. However, here is the regular expression in the operation of Linux system, that is, the Linux regular expression, the most commonly used regular expression command is grep (Egrep-
), SED, awk, in other words, the Three Musketeers of Linux to be able to work more efficiently, that must be inseparable from the regular expression of cooperation.
B. Regular expressions and our usual wildcard characters are fundamentally different, which is important to note. For example: in wildcards, ls. Log here is a wildcard character (all), not a regular expression.
To be prepared:
Alias grep= ' grep--color=auto '
Export Lc_all=c
Case one:
^ Use with $, note is case-sensitive.
| Regular Expressions |
Description |
| . ^i |
Matches the content that begins with I. Vi/vim Editor ^ represents the beginning of a line |
| x$ |
Matches the content that begins with X. Vi/vim Editor $ Represents the end of a line |
| ^$ |
Can be understood as the beginning of the end, that is, a blank line |
Case TWO:
| Regular Expressions |
Description |
| . |
Represents and can only represent any one character |
| \ |
Escape symbols |
| * |
Repeat 0 or more of the preceding characters |
| .* |
Match all characters |
| ^.* |
Start with any character |
| .*$ |
End with any character |
Summary of the meaning of the point (.)
1. Current directory
2. Yes the file is effective equivalent to source
3. Hide Files
4. Any one character (grep regular)
Two parameters in grep-i-o:-I means that the filtered content is not case sensitive, and-o means that only the filtered characters are displayed.
Case THREE:
| Regular Expressions |
Description |
| [ABC] |
Match any one of the characters in the character set [A-za-z], [0-9] |
| [^ABC] |
Match content that does not contain any of the following characters |
| A{N,M} |
Repeats N to M times, before a repeating character. If you use Egrep/sed-r, you can remove the slash |
| A{n,} |
Repeats at least n times, preceding a repeating character. If you use Egrep/sed-r, you can remove the slash |
| A{n} |
Repeats n times, before a repeating character. If you use Egrep/sed-r, you can remove the slash |
| A{,M} |
Repeats up to M times, before a repeating character. If you use Egrep/sed-r, you can remove the slash |
Linux Regular expressions